ClangASTContext.cpp revision f2a0a5c6c0ccf8c75a3409024d088646ed211726
1//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#include "lldb/Symbol/ClangASTContext.h" 11 12// C Includes 13// C++ Includes 14#include <string> 15 16// Other libraries and framework includes 17 18// Clang headers like to use NDEBUG inside of them to enable/disable debug 19// releated features using "#ifndef NDEBUG" preprocessor blocks to do one thing 20// or another. This is bad because it means that if clang was built in release 21// mode, it assumes that you are building in release mode which is not always 22// the case. You can end up with functions that are defined as empty in header 23// files when NDEBUG is not defined, and this can cause link errors with the 24// clang .a files that you have since you might be missing functions in the .a 25// file. So we have to define NDEBUG when including clang headers to avoid any 26// mismatches. This is covered by rdar://problem/8691220 27 28#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF) 29#define LLDB_DEFINED_NDEBUG_FOR_CLANG 30#define NDEBUG 31// Need to include assert.h so it is as clang would expect it to be (disabled) 32#include <assert.h> 33#endif 34 35#include "clang/AST/ASTContext.h" 36#include "clang/AST/ASTImporter.h" 37#include "clang/AST/CXXInheritance.h" 38#include "clang/AST/DeclObjC.h" 39#include "clang/AST/DeclTemplate.h" 40#include "clang/AST/RecordLayout.h" 41#include "clang/AST/Type.h" 42#include "clang/Basic/Builtins.h" 43#include "clang/Basic/FileManager.h" 44#include "clang/Basic/FileSystemOptions.h" 45#include "clang/Basic/SourceManager.h" 46#include "clang/Basic/TargetInfo.h" 47#include "clang/Basic/TargetOptions.h" 48#include "clang/Frontend/FrontendOptions.h" 49#include "clang/Frontend/LangStandard.h" 50 51#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG 52#undef NDEBUG 53#undef LLDB_DEFINED_NDEBUG_FOR_CLANG 54// Need to re-include assert.h so it is as _we_ would expect it to be (enabled) 55#include <assert.h> 56#endif 57 58#include "lldb/Core/ArchSpec.h" 59#include "lldb/Core/dwarf.h" 60#include "lldb/Core/Flags.h" 61#include "lldb/Core/Log.h" 62#include "lldb/Core/RegularExpression.h" 63#include "lldb/Expression/ASTDumper.h" 64#include "lldb/Symbol/VerifyDecl.h" 65#include "lldb/Target/ExecutionContext.h" 66#include "lldb/Target/Process.h" 67#include "lldb/Target/ObjCLanguageRuntime.h" 68 69 70#include <stdio.h> 71 72using namespace lldb; 73using namespace lldb_private; 74using namespace llvm; 75using namespace clang; 76 77 78static bool 79GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type) 80{ 81 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 82 switch (type_class) 83 { 84 case clang::Type::Record: 85 case clang::Type::Enum: 86 { 87 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type.getTypePtr()); 88 if (tag_type) 89 { 90 clang::TagDecl *tag_decl = tag_type->getDecl(); 91 if (tag_decl) 92 { 93 if (tag_decl->getDefinition()) 94 return true; 95 96 if (tag_decl->hasExternalLexicalStorage()) 97 { 98 if (ast) 99 { 100 ExternalASTSource *external_ast_source = ast->getExternalSource(); 101 if (external_ast_source) 102 { 103 external_ast_source->CompleteType(tag_decl); 104 return !tag_type->isIncompleteType(); 105 } 106 } 107 } 108 return false; 109 } 110 } 111 112 } 113 break; 114 115 case clang::Type::ObjCObject: 116 case clang::Type::ObjCInterface: 117 { 118 const clang::ObjCObjectType *objc_class_type = dyn_cast<clang::ObjCObjectType>(qual_type); 119 if (objc_class_type) 120 { 121 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 122 // We currently can't complete objective C types through the newly added ASTContext 123 // because it only supports TagDecl objects right now... 124 if (class_interface_decl) 125 { 126 bool is_forward_decl = class_interface_decl->isForwardDecl(); 127 if (is_forward_decl && class_interface_decl->hasExternalLexicalStorage()) 128 { 129 if (ast) 130 { 131 ExternalASTSource *external_ast_source = ast->getExternalSource(); 132 if (external_ast_source) 133 { 134 external_ast_source->CompleteType (class_interface_decl); 135 is_forward_decl = class_interface_decl->isForwardDecl(); 136 } 137 } 138 return is_forward_decl == false; 139 } 140 return true; 141 } 142 else 143 return false; 144 } 145 } 146 break; 147 148 case clang::Type::Typedef: 149 return GetCompleteQualType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()); 150 151 case clang::Type::Elaborated: 152 return GetCompleteQualType (ast, cast<ElaboratedType>(qual_type)->getNamedType()); 153 154 default: 155 break; 156 } 157 158 return true; 159} 160 161 162static AccessSpecifier 163ConvertAccessTypeToAccessSpecifier (AccessType access) 164{ 165 switch (access) 166 { 167 default: break; 168 case eAccessNone: return AS_none; 169 case eAccessPublic: return AS_public; 170 case eAccessPrivate: return AS_private; 171 case eAccessProtected: return AS_protected; 172 } 173 return AS_none; 174} 175 176static ObjCIvarDecl::AccessControl 177ConvertAccessTypeToObjCIvarAccessControl (AccessType access) 178{ 179 switch (access) 180 { 181 default: break; 182 case eAccessNone: return ObjCIvarDecl::None; 183 case eAccessPublic: return ObjCIvarDecl::Public; 184 case eAccessPrivate: return ObjCIvarDecl::Private; 185 case eAccessProtected: return ObjCIvarDecl::Protected; 186 case eAccessPackage: return ObjCIvarDecl::Package; 187 } 188 return ObjCIvarDecl::None; 189} 190 191 192static void 193ParseLangArgs 194( 195 LangOptions &Opts, 196 InputKind IK 197) 198{ 199 // FIXME: Cleanup per-file based stuff. 200 201 // Set some properties which depend soley on the input kind; it would be nice 202 // to move these to the language standard, and have the driver resolve the 203 // input kind + language standard. 204 if (IK == IK_Asm) { 205 Opts.AsmPreprocessor = 1; 206 } else if (IK == IK_ObjC || 207 IK == IK_ObjCXX || 208 IK == IK_PreprocessedObjC || 209 IK == IK_PreprocessedObjCXX) { 210 Opts.ObjC1 = Opts.ObjC2 = 1; 211 } 212 213 LangStandard::Kind LangStd = LangStandard::lang_unspecified; 214 215 if (LangStd == LangStandard::lang_unspecified) { 216 // Based on the base language, pick one. 217 switch (IK) { 218 case IK_None: 219 case IK_AST: 220 case IK_LLVM_IR: 221 assert (!"Invalid input kind!"); 222 case IK_OpenCL: 223 LangStd = LangStandard::lang_opencl; 224 break; 225 case IK_CUDA: 226 LangStd = LangStandard::lang_cuda; 227 break; 228 case IK_Asm: 229 case IK_C: 230 case IK_PreprocessedC: 231 case IK_ObjC: 232 case IK_PreprocessedObjC: 233 LangStd = LangStandard::lang_gnu99; 234 break; 235 case IK_CXX: 236 case IK_PreprocessedCXX: 237 case IK_ObjCXX: 238 case IK_PreprocessedObjCXX: 239 LangStd = LangStandard::lang_gnucxx98; 240 break; 241 } 242 } 243 244 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd); 245 Opts.BCPLComment = Std.hasBCPLComments(); 246 Opts.C99 = Std.isC99(); 247 Opts.CPlusPlus = Std.isCPlusPlus(); 248 Opts.CPlusPlus0x = Std.isCPlusPlus0x(); 249 Opts.Digraphs = Std.hasDigraphs(); 250 Opts.GNUMode = Std.isGNUMode(); 251 Opts.GNUInline = !Std.isC99(); 252 Opts.HexFloats = Std.hasHexFloats(); 253 Opts.ImplicitInt = Std.hasImplicitInt(); 254 255 // OpenCL has some additional defaults. 256 if (LangStd == LangStandard::lang_opencl) { 257 Opts.OpenCL = 1; 258 Opts.AltiVec = 1; 259 Opts.CXXOperatorNames = 1; 260 Opts.LaxVectorConversions = 1; 261 } 262 263 // OpenCL and C++ both have bool, true, false keywords. 264 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus; 265 266// if (Opts.CPlusPlus) 267// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names); 268// 269// if (Args.hasArg(OPT_fobjc_gc_only)) 270// Opts.setGCMode(LangOptions::GCOnly); 271// else if (Args.hasArg(OPT_fobjc_gc)) 272// Opts.setGCMode(LangOptions::HybridGC); 273// 274// if (Args.hasArg(OPT_print_ivar_layout)) 275// Opts.ObjCGCBitmapPrint = 1; 276// 277// if (Args.hasArg(OPT_faltivec)) 278// Opts.AltiVec = 1; 279// 280// if (Args.hasArg(OPT_pthread)) 281// Opts.POSIXThreads = 1; 282// 283// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility, 284// "default"); 285// if (Vis == "default") 286 Opts.setVisibilityMode(DefaultVisibility); 287// else if (Vis == "hidden") 288// Opts.setVisibilityMode(LangOptions::Hidden); 289// else if (Vis == "protected") 290// Opts.setVisibilityMode(LangOptions::Protected); 291// else 292// Diags.Report(diag::err_drv_invalid_value) 293// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis; 294 295// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv); 296 297 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs 298 // is specified, or -std is set to a conforming mode. 299 Opts.Trigraphs = !Opts.GNUMode; 300// if (Args.hasArg(OPT_trigraphs)) 301// Opts.Trigraphs = 1; 302// 303// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers, 304// OPT_fno_dollars_in_identifiers, 305// !Opts.AsmPreprocessor); 306// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings); 307// Opts.Microsoft = Args.hasArg(OPT_fms_extensions); 308// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings); 309// if (Args.hasArg(OPT_fno_lax_vector_conversions)) 310// Opts.LaxVectorConversions = 0; 311// Opts.Exceptions = Args.hasArg(OPT_fexceptions); 312// Opts.RTTI = !Args.hasArg(OPT_fno_rtti); 313// Opts.Blocks = Args.hasArg(OPT_fblocks); 314// Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char); 315// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar); 316// Opts.Freestanding = Args.hasArg(OPT_ffreestanding); 317// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding; 318// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new); 319// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions); 320// Opts.AccessControl = Args.hasArg(OPT_faccess_control); 321// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors); 322// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno); 323// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99, 324// Diags); 325// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime); 326// Opts.ObjCConstantStringClass = getLastArgValue(Args, 327// OPT_fconstant_string_class); 328// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi); 329// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior); 330// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls); 331// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags); 332// Opts.Static = Args.hasArg(OPT_static_define); 333 Opts.OptimizeSize = 0; 334 335 // FIXME: Eliminate this dependency. 336// unsigned Opt = 337// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags); 338// Opts.Optimize = Opt != 0; 339 unsigned Opt = 0; 340 341 // This is the __NO_INLINE__ define, which just depends on things like the 342 // optimization level and -fno-inline, not actually whether the backend has 343 // inlining enabled. 344 // 345 // FIXME: This is affected by other options (-fno-inline). 346 Opts.NoInline = !Opt; 347 348// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags); 349// switch (SSP) { 350// default: 351// Diags.Report(diag::err_drv_invalid_value) 352// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP; 353// break; 354// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break; 355// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break; 356// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break; 357// } 358} 359 360 361ClangASTContext::ClangASTContext (const char *target_triple) : 362 m_target_triple(), 363 m_ast_ap(), 364 m_language_options_ap(), 365 m_source_manager_ap(), 366 m_diagnostics_engine_ap(), 367 m_target_options_ap(), 368 m_target_info_ap(), 369 m_identifier_table_ap(), 370 m_selector_table_ap(), 371 m_builtins_ap(), 372 m_callback_tag_decl (NULL), 373 m_callback_objc_decl (NULL), 374 m_callback_baton (NULL) 375 376{ 377 if (target_triple && target_triple[0]) 378 SetTargetTriple (target_triple); 379} 380 381//---------------------------------------------------------------------- 382// Destructor 383//---------------------------------------------------------------------- 384ClangASTContext::~ClangASTContext() 385{ 386 m_builtins_ap.reset(); 387 m_selector_table_ap.reset(); 388 m_identifier_table_ap.reset(); 389 m_target_info_ap.reset(); 390 m_target_options_ap.reset(); 391 m_diagnostics_engine_ap.reset(); 392 m_source_manager_ap.reset(); 393 m_language_options_ap.reset(); 394 m_ast_ap.reset(); 395} 396 397 398void 399ClangASTContext::Clear() 400{ 401 m_ast_ap.reset(); 402 m_language_options_ap.reset(); 403 m_source_manager_ap.reset(); 404 m_diagnostics_engine_ap.reset(); 405 m_target_options_ap.reset(); 406 m_target_info_ap.reset(); 407 m_identifier_table_ap.reset(); 408 m_selector_table_ap.reset(); 409 m_builtins_ap.reset(); 410} 411 412const char * 413ClangASTContext::GetTargetTriple () 414{ 415 return m_target_triple.c_str(); 416} 417 418void 419ClangASTContext::SetTargetTriple (const char *target_triple) 420{ 421 Clear(); 422 m_target_triple.assign(target_triple); 423} 424 425void 426ClangASTContext::SetArchitecture (const ArchSpec &arch) 427{ 428 SetTargetTriple(arch.GetTriple().str().c_str()); 429} 430 431bool 432ClangASTContext::HasExternalSource () 433{ 434 ASTContext *ast = getASTContext(); 435 if (ast) 436 return ast->getExternalSource () != NULL; 437 return false; 438} 439 440void 441ClangASTContext::SetExternalSource (llvm::OwningPtr<ExternalASTSource> &ast_source_ap) 442{ 443 ASTContext *ast = getASTContext(); 444 if (ast) 445 { 446 ast->setExternalSource (ast_source_ap); 447 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true); 448 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true); 449 } 450} 451 452void 453ClangASTContext::RemoveExternalSource () 454{ 455 ASTContext *ast = getASTContext(); 456 457 if (ast) 458 { 459 llvm::OwningPtr<ExternalASTSource> empty_ast_source_ap; 460 ast->setExternalSource (empty_ast_source_ap); 461 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false); 462 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false); 463 } 464} 465 466 467 468ASTContext * 469ClangASTContext::getASTContext() 470{ 471 if (m_ast_ap.get() == NULL) 472 { 473 m_ast_ap.reset(new ASTContext (*getLanguageOptions(), 474 *getSourceManager(), 475 getTargetInfo(), 476 *getIdentifierTable(), 477 *getSelectorTable(), 478 *getBuiltinContext(), 479 0)); 480 481 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) 482 { 483 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage(); 484 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage(); 485 } 486 487 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false); 488 } 489 return m_ast_ap.get(); 490} 491 492Builtin::Context * 493ClangASTContext::getBuiltinContext() 494{ 495 if (m_builtins_ap.get() == NULL) 496 m_builtins_ap.reset (new Builtin::Context()); 497 return m_builtins_ap.get(); 498} 499 500IdentifierTable * 501ClangASTContext::getIdentifierTable() 502{ 503 if (m_identifier_table_ap.get() == NULL) 504 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL)); 505 return m_identifier_table_ap.get(); 506} 507 508LangOptions * 509ClangASTContext::getLanguageOptions() 510{ 511 if (m_language_options_ap.get() == NULL) 512 { 513 m_language_options_ap.reset(new LangOptions()); 514 ParseLangArgs(*m_language_options_ap, IK_ObjCXX); 515// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX); 516 } 517 return m_language_options_ap.get(); 518} 519 520SelectorTable * 521ClangASTContext::getSelectorTable() 522{ 523 if (m_selector_table_ap.get() == NULL) 524 m_selector_table_ap.reset (new SelectorTable()); 525 return m_selector_table_ap.get(); 526} 527 528clang::FileManager * 529ClangASTContext::getFileManager() 530{ 531 if (m_file_manager_ap.get() == NULL) 532 { 533 clang::FileSystemOptions file_system_options; 534 m_file_manager_ap.reset(new clang::FileManager(file_system_options)); 535 } 536 return m_file_manager_ap.get(); 537} 538 539clang::SourceManager * 540ClangASTContext::getSourceManager() 541{ 542 if (m_source_manager_ap.get() == NULL) 543 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager())); 544 return m_source_manager_ap.get(); 545} 546 547clang::DiagnosticsEngine * 548ClangASTContext::getDiagnosticsEngine() 549{ 550 if (m_diagnostics_engine_ap.get() == NULL) 551 { 552 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs()); 553 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp)); 554 } 555 return m_diagnostics_engine_ap.get(); 556} 557 558class NullDiagnosticConsumer : public DiagnosticConsumer 559{ 560public: 561 NullDiagnosticConsumer () 562 { 563 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); 564 } 565 566 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info) 567 { 568 if (m_log) 569 { 570 llvm::SmallVectorImpl<char> diag_str(10); 571 info.FormatDiagnostic(diag_str); 572 diag_str.push_back('\0'); 573 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data()); 574 } 575 } 576 577 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const 578 { 579 return new NullDiagnosticConsumer (); 580 } 581private: 582 LogSP m_log; 583}; 584 585DiagnosticConsumer * 586ClangASTContext::getDiagnosticConsumer() 587{ 588 if (m_diagnostic_consumer_ap.get() == NULL) 589 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer); 590 591 return m_diagnostic_consumer_ap.get(); 592} 593 594TargetOptions * 595ClangASTContext::getTargetOptions() 596{ 597 if (m_target_options_ap.get() == NULL && !m_target_triple.empty()) 598 { 599 m_target_options_ap.reset (new TargetOptions()); 600 if (m_target_options_ap.get()) 601 m_target_options_ap->Triple = m_target_triple; 602 } 603 return m_target_options_ap.get(); 604} 605 606 607TargetInfo * 608ClangASTContext::getTargetInfo() 609{ 610 // target_triple should be something like "x86_64-apple-darwin10" 611 if (m_target_info_ap.get() == NULL && !m_target_triple.empty()) 612 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), *getTargetOptions())); 613 return m_target_info_ap.get(); 614} 615 616#pragma mark Basic Types 617 618static inline bool 619QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type) 620{ 621 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type); 622 if (qual_type_bit_size == bit_size) 623 return true; 624 return false; 625} 626 627clang_type_t 628ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size) 629{ 630 ASTContext *ast = getASTContext(); 631 632 assert (ast != NULL); 633 634 return GetBuiltinTypeForEncodingAndBitSize (ast, encoding, bit_size); 635} 636 637clang_type_t 638ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size) 639{ 640 if (!ast) 641 return NULL; 642 643 switch (encoding) 644 { 645 case eEncodingInvalid: 646 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy)) 647 return ast->VoidPtrTy.getAsOpaquePtr(); 648 break; 649 650 case eEncodingUint: 651 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 652 return ast->UnsignedCharTy.getAsOpaquePtr(); 653 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 654 return ast->UnsignedShortTy.getAsOpaquePtr(); 655 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy)) 656 return ast->UnsignedIntTy.getAsOpaquePtr(); 657 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy)) 658 return ast->UnsignedLongTy.getAsOpaquePtr(); 659 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy)) 660 return ast->UnsignedLongLongTy.getAsOpaquePtr(); 661 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty)) 662 return ast->UnsignedInt128Ty.getAsOpaquePtr(); 663 break; 664 665 case eEncodingSint: 666 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy)) 667 return ast->CharTy.getAsOpaquePtr(); 668 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy)) 669 return ast->ShortTy.getAsOpaquePtr(); 670 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy)) 671 return ast->IntTy.getAsOpaquePtr(); 672 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy)) 673 return ast->LongTy.getAsOpaquePtr(); 674 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy)) 675 return ast->LongLongTy.getAsOpaquePtr(); 676 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty)) 677 return ast->Int128Ty.getAsOpaquePtr(); 678 break; 679 680 case eEncodingIEEE754: 681 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy)) 682 return ast->FloatTy.getAsOpaquePtr(); 683 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy)) 684 return ast->DoubleTy.getAsOpaquePtr(); 685 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy)) 686 return ast->LongDoubleTy.getAsOpaquePtr(); 687 break; 688 689 case eEncodingVector: 690 default: 691 break; 692 } 693 694 return NULL; 695} 696 697clang_type_t 698ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size) 699{ 700 ASTContext *ast = getASTContext(); 701 702 #define streq(a,b) strcmp(a,b) == 0 703 assert (ast != NULL); 704 if (ast) 705 { 706 switch (dw_ate) 707 { 708 default: 709 break; 710 711 case DW_ATE_address: 712 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy)) 713 return ast->VoidPtrTy.getAsOpaquePtr(); 714 break; 715 716 case DW_ATE_boolean: 717 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy)) 718 return ast->BoolTy.getAsOpaquePtr(); 719 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 720 return ast->UnsignedCharTy.getAsOpaquePtr(); 721 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 722 return ast->UnsignedShortTy.getAsOpaquePtr(); 723 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy)) 724 return ast->UnsignedIntTy.getAsOpaquePtr(); 725 break; 726 727 case DW_ATE_lo_user: 728 // This has been seen to mean DW_AT_complex_integer 729 if (type_name) 730 { 731 if (::strstr(type_name, "complex")) 732 { 733 clang_type_t complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2); 734 return ast->getComplexType (QualType::getFromOpaquePtr(complex_int_clang_type)).getAsOpaquePtr(); 735 } 736 } 737 break; 738 739 case DW_ATE_complex_float: 740 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy)) 741 return ast->FloatComplexTy.getAsOpaquePtr(); 742 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy)) 743 return ast->DoubleComplexTy.getAsOpaquePtr(); 744 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy)) 745 return ast->LongDoubleComplexTy.getAsOpaquePtr(); 746 else 747 { 748 clang_type_t complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2); 749 return ast->getComplexType (QualType::getFromOpaquePtr(complex_float_clang_type)).getAsOpaquePtr(); 750 } 751 break; 752 753 case DW_ATE_float: 754 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy)) 755 return ast->FloatTy.getAsOpaquePtr(); 756 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy)) 757 return ast->DoubleTy.getAsOpaquePtr(); 758 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy)) 759 return ast->LongDoubleTy.getAsOpaquePtr(); 760 break; 761 762 case DW_ATE_signed: 763 if (type_name) 764 { 765 if (strstr(type_name, "long long")) 766 { 767 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy)) 768 return ast->LongLongTy.getAsOpaquePtr(); 769 } 770 else if (strstr(type_name, "long")) 771 { 772 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy)) 773 return ast->LongTy.getAsOpaquePtr(); 774 } 775 else if (strstr(type_name, "short")) 776 { 777 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy)) 778 return ast->ShortTy.getAsOpaquePtr(); 779 } 780 else if (strstr(type_name, "char")) 781 { 782 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy)) 783 return ast->CharTy.getAsOpaquePtr(); 784 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy)) 785 return ast->SignedCharTy.getAsOpaquePtr(); 786 } 787 else if (strstr(type_name, "int")) 788 { 789 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy)) 790 return ast->IntTy.getAsOpaquePtr(); 791 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty)) 792 return ast->Int128Ty.getAsOpaquePtr(); 793 } 794 else if (streq(type_name, "wchar_t")) 795 { 796 if (QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy)) 797 return ast->WCharTy.getAsOpaquePtr(); 798 } 799 else if (streq(type_name, "void")) 800 { 801 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy)) 802 return ast->VoidTy.getAsOpaquePtr(); 803 } 804 } 805 // We weren't able to match up a type name, just search by size 806 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy)) 807 return ast->CharTy.getAsOpaquePtr(); 808 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy)) 809 return ast->ShortTy.getAsOpaquePtr(); 810 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy)) 811 return ast->IntTy.getAsOpaquePtr(); 812 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy)) 813 return ast->LongTy.getAsOpaquePtr(); 814 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy)) 815 return ast->LongLongTy.getAsOpaquePtr(); 816 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty)) 817 return ast->Int128Ty.getAsOpaquePtr(); 818 break; 819 820 case DW_ATE_signed_char: 821 if (type_name) 822 { 823 if (streq(type_name, "signed char")) 824 { 825 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy)) 826 return ast->SignedCharTy.getAsOpaquePtr(); 827 } 828 } 829 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy)) 830 return ast->CharTy.getAsOpaquePtr(); 831 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy)) 832 return ast->SignedCharTy.getAsOpaquePtr(); 833 break; 834 835 case DW_ATE_unsigned: 836 if (type_name) 837 { 838 if (strstr(type_name, "long long")) 839 { 840 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy)) 841 return ast->UnsignedLongLongTy.getAsOpaquePtr(); 842 } 843 else if (strstr(type_name, "long")) 844 { 845 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy)) 846 return ast->UnsignedLongTy.getAsOpaquePtr(); 847 } 848 else if (strstr(type_name, "short")) 849 { 850 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 851 return ast->UnsignedShortTy.getAsOpaquePtr(); 852 } 853 else if (strstr(type_name, "char")) 854 { 855 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 856 return ast->UnsignedCharTy.getAsOpaquePtr(); 857 } 858 else if (strstr(type_name, "int")) 859 { 860 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy)) 861 return ast->UnsignedIntTy.getAsOpaquePtr(); 862 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty)) 863 return ast->UnsignedInt128Ty.getAsOpaquePtr(); 864 } 865 } 866 // We weren't able to match up a type name, just search by size 867 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 868 return ast->UnsignedCharTy.getAsOpaquePtr(); 869 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 870 return ast->UnsignedShortTy.getAsOpaquePtr(); 871 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy)) 872 return ast->UnsignedIntTy.getAsOpaquePtr(); 873 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy)) 874 return ast->UnsignedLongTy.getAsOpaquePtr(); 875 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy)) 876 return ast->UnsignedLongLongTy.getAsOpaquePtr(); 877 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty)) 878 return ast->UnsignedInt128Ty.getAsOpaquePtr(); 879 break; 880 881 case DW_ATE_unsigned_char: 882 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 883 return ast->UnsignedCharTy.getAsOpaquePtr(); 884 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 885 return ast->UnsignedShortTy.getAsOpaquePtr(); 886 break; 887 888 case DW_ATE_imaginary_float: 889 break; 890 891 case DW_ATE_UTF: 892 if (type_name) 893 { 894 if (streq(type_name, "char16_t")) 895 { 896 return ast->Char16Ty.getAsOpaquePtr(); 897 } 898 else if (streq(type_name, "char32_t")) 899 { 900 return ast->Char32Ty.getAsOpaquePtr(); 901 } 902 } 903 break; 904 } 905 } 906 // This assert should fire for anything that we don't catch above so we know 907 // to fix any issues we run into. 908 if (type_name) 909 { 910 fprintf (stderr, "error: need to add support for DW_TAG_base_type '%s' encoded with DW_ATE = 0x%x, bit_size = %u\n", type_name, dw_ate, bit_size); 911 } 912 else 913 { 914 fprintf (stderr, "error: need to add support for DW_TAG_base_type encoded with DW_ATE = 0x%x, bit_size = %u\n", dw_ate, bit_size); 915 } 916 return NULL; 917} 918 919clang_type_t 920ClangASTContext::GetBuiltInType_void(ASTContext *ast) 921{ 922 return ast->VoidTy.getAsOpaquePtr(); 923} 924 925clang_type_t 926ClangASTContext::GetBuiltInType_bool() 927{ 928 return getASTContext()->BoolTy.getAsOpaquePtr(); 929} 930 931clang_type_t 932ClangASTContext::GetBuiltInType_objc_id() 933{ 934 return getASTContext()->getPointerType(getASTContext()->ObjCBuiltinIdTy).getAsOpaquePtr(); 935} 936 937clang_type_t 938ClangASTContext::GetBuiltInType_objc_Class() 939{ 940 return getASTContext()->getPointerType(getASTContext()->ObjCBuiltinClassTy).getAsOpaquePtr(); 941} 942 943clang_type_t 944ClangASTContext::GetBuiltInType_objc_selector() 945{ 946 return getASTContext()->getPointerType(getASTContext()->ObjCBuiltinSelTy).getAsOpaquePtr(); 947} 948 949clang_type_t 950ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) 951{ 952 return ast->UnknownAnyTy.getAsOpaquePtr(); 953} 954 955clang_type_t 956ClangASTContext::GetCStringType (bool is_const) 957{ 958 QualType char_type(getASTContext()->CharTy); 959 960 if (is_const) 961 char_type.addConst(); 962 963 return getASTContext()->getPointerType(char_type).getAsOpaquePtr(); 964} 965 966clang_type_t 967ClangASTContext::GetVoidPtrType (bool is_const) 968{ 969 return GetVoidPtrType(getASTContext(), is_const); 970} 971 972clang_type_t 973ClangASTContext::GetVoidPtrType (ASTContext *ast, bool is_const) 974{ 975 QualType void_ptr_type(ast->VoidPtrTy); 976 977 if (is_const) 978 void_ptr_type.addConst(); 979 980 return void_ptr_type.getAsOpaquePtr(); 981} 982 983clang_type_t 984ClangASTContext::CopyType (ASTContext *dst_ast, 985 ASTContext *src_ast, 986 clang_type_t clang_type) 987{ 988 FileSystemOptions file_system_options; 989 FileManager file_manager (file_system_options); 990 ASTImporter importer(*dst_ast, file_manager, 991 *src_ast, file_manager, 992 false); 993 994 QualType src (QualType::getFromOpaquePtr(clang_type)); 995 QualType dst (importer.Import(src)); 996 997 return dst.getAsOpaquePtr(); 998} 999 1000 1001clang::Decl * 1002ClangASTContext::CopyDecl (ASTContext *dst_ast, 1003 ASTContext *src_ast, 1004 clang::Decl *source_decl) 1005{ 1006 FileSystemOptions file_system_options; 1007 FileManager file_manager (file_system_options); 1008 ASTImporter importer(*dst_ast, file_manager, 1009 *src_ast, file_manager, 1010 false); 1011 1012 return importer.Import(source_decl); 1013} 1014 1015bool 1016ClangASTContext::AreTypesSame(ASTContext *ast, 1017 clang_type_t type1, 1018 clang_type_t type2) 1019{ 1020 return ast->hasSameType (QualType::getFromOpaquePtr(type1), 1021 QualType::getFromOpaquePtr(type2)); 1022} 1023 1024#pragma mark CVR modifiers 1025 1026clang_type_t 1027ClangASTContext::AddConstModifier (clang_type_t clang_type) 1028{ 1029 if (clang_type) 1030 { 1031 QualType result(QualType::getFromOpaquePtr(clang_type)); 1032 result.addConst(); 1033 return result.getAsOpaquePtr(); 1034 } 1035 return NULL; 1036} 1037 1038clang_type_t 1039ClangASTContext::AddRestrictModifier (clang_type_t clang_type) 1040{ 1041 if (clang_type) 1042 { 1043 QualType result(QualType::getFromOpaquePtr(clang_type)); 1044 result.getQualifiers().setRestrict (true); 1045 return result.getAsOpaquePtr(); 1046 } 1047 return NULL; 1048} 1049 1050clang_type_t 1051ClangASTContext::AddVolatileModifier (clang_type_t clang_type) 1052{ 1053 if (clang_type) 1054 { 1055 QualType result(QualType::getFromOpaquePtr(clang_type)); 1056 result.getQualifiers().setVolatile (true); 1057 return result.getAsOpaquePtr(); 1058 } 1059 return NULL; 1060} 1061 1062 1063clang_type_t 1064ClangASTContext::GetTypeForDecl (TagDecl *decl) 1065{ 1066 // No need to call the getASTContext() accessor (which can create the AST 1067 // if it isn't created yet, because we can't have created a decl in this 1068 // AST if our AST didn't already exist... 1069 if (m_ast_ap.get()) 1070 return m_ast_ap->getTagDeclType(decl).getAsOpaquePtr(); 1071 return NULL; 1072} 1073 1074clang_type_t 1075ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl) 1076{ 1077 // No need to call the getASTContext() accessor (which can create the AST 1078 // if it isn't created yet, because we can't have created a decl in this 1079 // AST if our AST didn't already exist... 1080 if (m_ast_ap.get()) 1081 return m_ast_ap->getObjCInterfaceType(decl).getAsOpaquePtr(); 1082 return NULL; 1083} 1084 1085#pragma mark Structure, Unions, Classes 1086 1087clang_type_t 1088ClangASTContext::CreateRecordType (DeclContext *decl_ctx, AccessType access_type, const char *name, int kind, LanguageType language) 1089{ 1090 ASTContext *ast = getASTContext(); 1091 assert (ast != NULL); 1092 1093 if (decl_ctx == NULL) 1094 decl_ctx = ast->getTranslationUnitDecl(); 1095 1096 1097 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus) 1098 { 1099 bool isForwardDecl = true; 1100 bool isInternal = false; 1101 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal); 1102 } 1103 1104 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and 1105 // we will need to update this code. I was told to currently always use 1106 // the CXXRecordDecl class since we often don't know from debug information 1107 // if something is struct or a class, so we default to always use the more 1108 // complete definition just in case. 1109 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast, 1110 (TagDecl::TagKind)kind, 1111 decl_ctx, 1112 SourceLocation(), 1113 SourceLocation(), 1114 name && name[0] ? &ast->Idents.get(name) : NULL); 1115 1116 if (decl_ctx) 1117 { 1118 if (access_type != eAccessNone) 1119 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type)); 1120 decl_ctx->addDecl (decl); 1121 } 1122 return ast->getTagDeclType(decl).getAsOpaquePtr(); 1123} 1124 1125ClassTemplateDecl * 1126ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx, 1127 lldb::AccessType access_type, 1128 const char *class_name, 1129 int kind, 1130 const TemplateParameterInfos &template_param_infos) 1131{ 1132 ASTContext *ast = getASTContext(); 1133 1134 ClassTemplateDecl *class_template_decl = NULL; 1135 if (decl_ctx == NULL) 1136 decl_ctx = ast->getTranslationUnitDecl(); 1137 1138 IdentifierInfo &identifier_info = ast->Idents.get(class_name); 1139 DeclarationName decl_name (&identifier_info); 1140 1141 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); 1142 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos) 1143 { 1144 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(*pos); 1145 if (class_template_decl) 1146 return class_template_decl; 1147 } 1148 1149 llvm::SmallVector<NamedDecl *, 8> template_param_decls; 1150 const bool parameter_pack = false; 1151 const bool is_typename = false; 1152 const unsigned depth = 0; 1153 const size_t num_template_params = template_param_infos.GetSize(); 1154 for (size_t i=0; i<num_template_params; ++i) 1155 { 1156 const char *name = template_param_infos.names[i]; 1157 if (template_param_infos.args[i].getAsIntegral()) 1158 { 1159 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast, 1160 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc, 1161 SourceLocation(), 1162 SourceLocation(), 1163 depth, 1164 i, 1165 &ast->Idents.get(name), 1166 template_param_infos.args[i].getAsType(), 1167 parameter_pack, 1168 NULL)); 1169 1170 } 1171 else 1172 { 1173 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast, 1174 ast->getTranslationUnitDecl(), // Is this the right decl context? 1175 SourceLocation(), 1176 SourceLocation(), 1177 depth, 1178 i, 1179 &ast->Idents.get(name), 1180 is_typename, 1181 parameter_pack)); 1182 } 1183 } 1184 1185 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast, 1186 SourceLocation(), 1187 SourceLocation(), 1188 &template_param_decls.front(), 1189 template_param_decls.size(), 1190 SourceLocation()); 1191 1192 1193 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast, 1194 (TagDecl::TagKind)kind, 1195 decl_ctx, // What decl context do we use here? TU? The actual decl context? 1196 SourceLocation(), 1197 SourceLocation(), 1198 &identifier_info); 1199 1200 1201 class_template_decl = ClassTemplateDecl::Create (*ast, 1202 decl_ctx, // What decl context do we use here? TU? The actual decl context? 1203 SourceLocation(), 1204 decl_name, 1205 template_param_list, 1206 template_cxx_decl, 1207 NULL); 1208 1209 if (class_template_decl) 1210 { 1211 if (access_type != eAccessNone) 1212 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type)); 1213 decl_ctx->addDecl (class_template_decl); 1214 1215#ifdef LLDB_CONFIGURATION_DEBUG 1216 VerifyDecl(class_template_decl); 1217#endif 1218 } 1219 1220 return class_template_decl; 1221} 1222 1223 1224ClassTemplateSpecializationDecl * 1225ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx, 1226 ClassTemplateDecl *class_template_decl, 1227 int kind, 1228 const TemplateParameterInfos &template_param_infos) 1229{ 1230 ASTContext *ast = getASTContext(); 1231 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast, 1232 (TagDecl::TagKind)kind, 1233 decl_ctx, 1234 SourceLocation(), 1235 SourceLocation(), 1236 class_template_decl, 1237 &template_param_infos.args.front(), 1238 template_param_infos.args.size(), 1239 NULL); 1240 1241 return class_template_specialization_decl; 1242} 1243 1244lldb::clang_type_t 1245ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl) 1246{ 1247 if (class_template_specialization_decl) 1248 { 1249 ASTContext *ast = getASTContext(); 1250 if (ast) 1251 return ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr(); 1252 } 1253 return NULL; 1254} 1255 1256bool 1257ClangASTContext::SetHasExternalStorage (clang_type_t clang_type, bool has_extern) 1258{ 1259 if (clang_type == NULL) 1260 return false; 1261 1262 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 1263 1264 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 1265 switch (type_class) 1266 { 1267 case clang::Type::Record: 1268 { 1269 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 1270 if (cxx_record_decl) 1271 { 1272 cxx_record_decl->setHasExternalLexicalStorage (has_extern); 1273 cxx_record_decl->setHasExternalVisibleStorage (has_extern); 1274 return true; 1275 } 1276 } 1277 break; 1278 1279 case clang::Type::Enum: 1280 { 1281 EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl(); 1282 if (enum_decl) 1283 { 1284 enum_decl->setHasExternalLexicalStorage (has_extern); 1285 enum_decl->setHasExternalVisibleStorage (has_extern); 1286 return true; 1287 } 1288 } 1289 break; 1290 1291 case clang::Type::ObjCObject: 1292 case clang::Type::ObjCInterface: 1293 { 1294 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 1295 assert (objc_class_type); 1296 if (objc_class_type) 1297 { 1298 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1299 1300 if (class_interface_decl) 1301 { 1302 class_interface_decl->setHasExternalLexicalStorage (has_extern); 1303 class_interface_decl->setHasExternalVisibleStorage (has_extern); 1304 return true; 1305 } 1306 } 1307 } 1308 break; 1309 1310 case clang::Type::Typedef: 1311 return ClangASTContext::SetHasExternalStorage (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern); 1312 1313 case clang::Type::Elaborated: 1314 return ClangASTContext::SetHasExternalStorage (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern); 1315 1316 default: 1317 break; 1318 } 1319 return false; 1320} 1321 1322static bool 1323IsOperator (const char *name, OverloadedOperatorKind &op_kind) 1324{ 1325 if (name == NULL || name[0] == '\0') 1326 return false; 1327 1328#define OPERATOR_PREFIX "operator" 1329#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1) 1330 1331 const char *post_op_name = NULL; 1332 1333 bool no_space = true; 1334 1335 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH)) 1336 return false; 1337 1338 post_op_name = name + OPERATOR_PREFIX_LENGTH; 1339 1340 if (post_op_name[0] == ' ') 1341 { 1342 post_op_name++; 1343 no_space = false; 1344 } 1345 1346#undef OPERATOR_PREFIX 1347#undef OPERATOR_PREFIX_LENGTH 1348 1349 // This is an operator, set the overloaded operator kind to invalid 1350 // in case this is a conversion operator... 1351 op_kind = NUM_OVERLOADED_OPERATORS; 1352 1353 switch (post_op_name[0]) 1354 { 1355 default: 1356 if (no_space) 1357 return false; 1358 break; 1359 case 'n': 1360 if (no_space) 1361 return false; 1362 if (strcmp (post_op_name, "new") == 0) 1363 op_kind = OO_New; 1364 else if (strcmp (post_op_name, "new[]") == 0) 1365 op_kind = OO_Array_New; 1366 break; 1367 1368 case 'd': 1369 if (no_space) 1370 return false; 1371 if (strcmp (post_op_name, "delete") == 0) 1372 op_kind = OO_Delete; 1373 else if (strcmp (post_op_name, "delete[]") == 0) 1374 op_kind = OO_Array_Delete; 1375 break; 1376 1377 case '+': 1378 if (post_op_name[1] == '\0') 1379 op_kind = OO_Plus; 1380 else if (post_op_name[2] == '\0') 1381 { 1382 if (post_op_name[1] == '=') 1383 op_kind = OO_PlusEqual; 1384 else if (post_op_name[1] == '+') 1385 op_kind = OO_PlusPlus; 1386 } 1387 break; 1388 1389 case '-': 1390 if (post_op_name[1] == '\0') 1391 op_kind = OO_Minus; 1392 else if (post_op_name[2] == '\0') 1393 { 1394 switch (post_op_name[1]) 1395 { 1396 case '=': op_kind = OO_MinusEqual; break; 1397 case '-': op_kind = OO_MinusMinus; break; 1398 case '>': op_kind = OO_Arrow; break; 1399 } 1400 } 1401 else if (post_op_name[3] == '\0') 1402 { 1403 if (post_op_name[2] == '*') 1404 op_kind = OO_ArrowStar; break; 1405 } 1406 break; 1407 1408 case '*': 1409 if (post_op_name[1] == '\0') 1410 op_kind = OO_Star; 1411 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 1412 op_kind = OO_StarEqual; 1413 break; 1414 1415 case '/': 1416 if (post_op_name[1] == '\0') 1417 op_kind = OO_Slash; 1418 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 1419 op_kind = OO_SlashEqual; 1420 break; 1421 1422 case '%': 1423 if (post_op_name[1] == '\0') 1424 op_kind = OO_Percent; 1425 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 1426 op_kind = OO_PercentEqual; 1427 break; 1428 1429 1430 case '^': 1431 if (post_op_name[1] == '\0') 1432 op_kind = OO_Caret; 1433 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 1434 op_kind = OO_CaretEqual; 1435 break; 1436 1437 case '&': 1438 if (post_op_name[1] == '\0') 1439 op_kind = OO_Amp; 1440 else if (post_op_name[2] == '\0') 1441 { 1442 switch (post_op_name[1]) 1443 { 1444 case '=': op_kind = OO_AmpEqual; break; 1445 case '&': op_kind = OO_AmpAmp; break; 1446 } 1447 } 1448 break; 1449 1450 case '|': 1451 if (post_op_name[1] == '\0') 1452 op_kind = OO_Pipe; 1453 else if (post_op_name[2] == '\0') 1454 { 1455 switch (post_op_name[1]) 1456 { 1457 case '=': op_kind = OO_PipeEqual; break; 1458 case '|': op_kind = OO_PipePipe; break; 1459 } 1460 } 1461 break; 1462 1463 case '~': 1464 if (post_op_name[1] == '\0') 1465 op_kind = OO_Tilde; 1466 break; 1467 1468 case '!': 1469 if (post_op_name[1] == '\0') 1470 op_kind = OO_Exclaim; 1471 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 1472 op_kind = OO_ExclaimEqual; 1473 break; 1474 1475 case '=': 1476 if (post_op_name[1] == '\0') 1477 op_kind = OO_Equal; 1478 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 1479 op_kind = OO_EqualEqual; 1480 break; 1481 1482 case '<': 1483 if (post_op_name[1] == '\0') 1484 op_kind = OO_Less; 1485 else if (post_op_name[2] == '\0') 1486 { 1487 switch (post_op_name[1]) 1488 { 1489 case '<': op_kind = OO_LessLess; break; 1490 case '=': op_kind = OO_LessEqual; break; 1491 } 1492 } 1493 else if (post_op_name[3] == '\0') 1494 { 1495 if (post_op_name[2] == '=') 1496 op_kind = OO_LessLessEqual; 1497 } 1498 break; 1499 1500 case '>': 1501 if (post_op_name[1] == '\0') 1502 op_kind = OO_Greater; 1503 else if (post_op_name[2] == '\0') 1504 { 1505 switch (post_op_name[1]) 1506 { 1507 case '>': op_kind = OO_GreaterGreater; break; 1508 case '=': op_kind = OO_GreaterEqual; break; 1509 } 1510 } 1511 else if (post_op_name[1] == '>' && 1512 post_op_name[2] == '=' && 1513 post_op_name[3] == '\0') 1514 { 1515 op_kind = OO_GreaterGreaterEqual; 1516 } 1517 break; 1518 1519 case ',': 1520 if (post_op_name[1] == '\0') 1521 op_kind = OO_Comma; 1522 break; 1523 1524 case '(': 1525 if (post_op_name[1] == ')' && post_op_name[2] == '\0') 1526 op_kind = OO_Call; 1527 break; 1528 1529 case '[': 1530 if (post_op_name[1] == ']' && post_op_name[2] == '\0') 1531 op_kind = OO_Subscript; 1532 break; 1533 } 1534 1535 return true; 1536} 1537 1538static inline bool 1539check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params) 1540{ 1541 // Special-case call since it can take any number of operands 1542 if(op_kind == OO_Call) 1543 return true; 1544 1545 // The parameter count doens't include "this" 1546 if (num_params == 0) 1547 return unary; 1548 if (num_params == 1) 1549 return binary; 1550 else 1551 return false; 1552} 1553 1554bool 1555ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params) 1556{ 1557#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) case OO_##Name: return check_op_param (op_kind, Unary, Binary, num_params); 1558 switch (op_kind) 1559 { 1560#include "clang/Basic/OperatorKinds.def" 1561 default: break; 1562 } 1563 return false; 1564} 1565 1566CXXMethodDecl * 1567ClangASTContext::AddMethodToCXXRecordType 1568( 1569 ASTContext *ast, 1570 clang_type_t record_opaque_type, 1571 const char *name, 1572 clang_type_t method_opaque_type, 1573 lldb::AccessType access, 1574 bool is_virtual, 1575 bool is_static, 1576 bool is_inline, 1577 bool is_explicit, 1578 bool is_attr_used, 1579 bool is_artificial 1580) 1581{ 1582 if (!record_opaque_type || !method_opaque_type || !name) 1583 return NULL; 1584 1585 assert(ast); 1586 1587 IdentifierTable *identifier_table = &ast->Idents; 1588 1589 assert(identifier_table); 1590 1591 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type)); 1592 1593 CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl(); 1594 1595 if (cxx_record_decl == NULL) 1596 return NULL; 1597 1598 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type)); 1599 1600 CXXMethodDecl *cxx_method_decl = NULL; 1601 1602 DeclarationName decl_name (&identifier_table->get(name)); 1603 1604 const clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr()); 1605 1606 if (function_Type == NULL) 1607 return NULL; 1608 1609 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type)); 1610 1611 if (!method_function_prototype) 1612 return NULL; 1613 1614 unsigned int num_params = method_function_prototype->getNumArgs(); 1615 1616 CXXDestructorDecl *cxx_dtor_decl(NULL); 1617 CXXConstructorDecl *cxx_ctor_decl(NULL); 1618 1619 if (name[0] == '~') 1620 { 1621 cxx_dtor_decl = CXXDestructorDecl::Create (*ast, 1622 cxx_record_decl, 1623 SourceLocation(), 1624 DeclarationNameInfo (ast->DeclarationNames.getCXXDestructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()), 1625 method_qual_type, 1626 NULL, 1627 is_inline, 1628 is_artificial); 1629 cxx_method_decl = cxx_dtor_decl; 1630 } 1631 else if (decl_name == cxx_record_decl->getDeclName()) 1632 { 1633 cxx_ctor_decl = CXXConstructorDecl::Create (*ast, 1634 cxx_record_decl, 1635 SourceLocation(), 1636 DeclarationNameInfo (ast->DeclarationNames.getCXXConstructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()), 1637 method_qual_type, 1638 NULL, // TypeSourceInfo * 1639 is_explicit, 1640 is_inline, 1641 is_artificial, 1642 false /*is_constexpr*/); 1643 cxx_method_decl = cxx_ctor_decl; 1644 } 1645 else 1646 { 1647 1648 OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS; 1649 if (IsOperator (name, op_kind)) 1650 { 1651 if (op_kind != NUM_OVERLOADED_OPERATORS) 1652 { 1653 // Check the number of operator parameters. Sometimes we have 1654 // seen bad DWARF that doesn't correctly describe operators and 1655 // if we try to create a methed and add it to the class, clang 1656 // will assert and crash, so we need to make sure things are 1657 // acceptable. 1658 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params)) 1659 return NULL; 1660 cxx_method_decl = CXXMethodDecl::Create (*ast, 1661 cxx_record_decl, 1662 SourceLocation(), 1663 DeclarationNameInfo (ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()), 1664 method_qual_type, 1665 NULL, // TypeSourceInfo * 1666 is_static, 1667 SC_None, 1668 is_inline, 1669 false /*is_constexpr*/, 1670 SourceLocation()); 1671 } 1672 else if (num_params == 0) 1673 { 1674 // Conversion operators don't take params... 1675 cxx_method_decl = CXXConversionDecl::Create (*ast, 1676 cxx_record_decl, 1677 SourceLocation(), 1678 DeclarationNameInfo (ast->DeclarationNames.getCXXConversionFunctionName (ast->getCanonicalType (function_Type->getResultType())), SourceLocation()), 1679 method_qual_type, 1680 NULL, // TypeSourceInfo * 1681 is_inline, 1682 is_explicit, 1683 false /*is_constexpr*/, 1684 SourceLocation()); 1685 } 1686 } 1687 1688 if (cxx_method_decl == NULL) 1689 { 1690 cxx_method_decl = CXXMethodDecl::Create (*ast, 1691 cxx_record_decl, 1692 SourceLocation(), 1693 DeclarationNameInfo (decl_name, SourceLocation()), 1694 method_qual_type, 1695 NULL, // TypeSourceInfo * 1696 is_static, 1697 SC_None, 1698 is_inline, 1699 false /*is_constexpr*/, 1700 SourceLocation()); 1701 } 1702 } 1703 1704 AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access); 1705 1706 cxx_method_decl->setAccess (access_specifier); 1707 cxx_method_decl->setVirtualAsWritten (is_virtual); 1708 1709 if (is_attr_used) 1710 cxx_method_decl->addAttr(::new (*ast) UsedAttr(SourceRange(), *ast)); 1711 1712 // Populate the method decl with parameter decls 1713 1714 llvm::SmallVector<ParmVarDecl *, 12> params; 1715 1716 for (int param_index = 0; 1717 param_index < num_params; 1718 ++param_index) 1719 { 1720 params.push_back (ParmVarDecl::Create (*ast, 1721 cxx_method_decl, 1722 SourceLocation(), 1723 SourceLocation(), 1724 NULL, // anonymous 1725 method_function_prototype->getArgType(param_index), 1726 NULL, 1727 SC_None, 1728 SC_None, 1729 NULL)); 1730 } 1731 1732 cxx_method_decl->setParams (ArrayRef<ParmVarDecl*>(params)); 1733 1734 cxx_record_decl->addDecl (cxx_method_decl); 1735 1736 // Sometimes the debug info will mention a constructor (default/copy/move), 1737 // destructor, or assignment operator (copy/move) but there won't be any 1738 // version of this in the code. So we check if the function was artificially 1739 // generated and if it is trivial and this lets the compiler/backend know 1740 // that it can inline the IR for these when it needs to and we can avoid a 1741 // "missing function" error when running expressions. 1742 1743 if (is_artificial) 1744 { 1745 if (cxx_ctor_decl && 1746 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) || 1747 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) || 1748 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) )) 1749 { 1750 cxx_ctor_decl->setDefaulted(); 1751 cxx_ctor_decl->setTrivial(true); 1752 } 1753 else if (cxx_dtor_decl) 1754 { 1755 if (cxx_record_decl->hasTrivialDestructor()) 1756 { 1757 cxx_dtor_decl->setDefaulted(); 1758 cxx_dtor_decl->setTrivial(true); 1759 } 1760 } 1761 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) || 1762 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment())) 1763 { 1764 cxx_method_decl->setDefaulted(); 1765 cxx_method_decl->setTrivial(true); 1766 } 1767 } 1768 1769#ifdef LLDB_CONFIGURATION_DEBUG 1770 VerifyDecl(cxx_method_decl); 1771#endif 1772 1773// printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic()); 1774// printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate()); 1775// printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD()); 1776// printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty()); 1777// printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract()); 1778// printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor()); 1779// printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor()); 1780// printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment()); 1781// printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor()); 1782 return cxx_method_decl; 1783} 1784 1785bool 1786ClangASTContext::AddFieldToRecordType 1787( 1788 ASTContext *ast, 1789 clang_type_t record_clang_type, 1790 const char *name, 1791 clang_type_t field_type, 1792 AccessType access, 1793 uint32_t bitfield_bit_size 1794) 1795{ 1796 if (record_clang_type == NULL || field_type == NULL) 1797 return false; 1798 1799 IdentifierTable *identifier_table = &ast->Idents; 1800 1801 assert (ast != NULL); 1802 assert (identifier_table != NULL); 1803 1804 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type)); 1805 1806 const clang::Type *clang_type = record_qual_type.getTypePtr(); 1807 if (clang_type) 1808 { 1809 const RecordType *record_type = dyn_cast<RecordType>(clang_type); 1810 1811 if (record_type) 1812 { 1813 RecordDecl *record_decl = record_type->getDecl(); 1814 1815 clang::Expr *bit_width = NULL; 1816 if (bitfield_bit_size != 0) 1817 { 1818 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size); 1819 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation()); 1820 } 1821 FieldDecl *field = FieldDecl::Create (*ast, 1822 record_decl, 1823 SourceLocation(), 1824 SourceLocation(), 1825 name ? &identifier_table->get(name) : NULL, // Identifier 1826 QualType::getFromOpaquePtr(field_type), // Field type 1827 NULL, // TInfo * 1828 bit_width, // BitWidth 1829 false, // Mutable 1830 false); // HasInit 1831 1832 field->setAccess (ConvertAccessTypeToAccessSpecifier (access)); 1833 1834 if (field) 1835 { 1836 record_decl->addDecl(field); 1837 1838#ifdef LLDB_CONFIGURATION_DEBUG 1839 VerifyDecl(field); 1840#endif 1841 } 1842 } 1843 else 1844 { 1845 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type); 1846 if (objc_class_type) 1847 { 1848 bool is_synthesized = false; 1849 ClangASTContext::AddObjCClassIVar (ast, 1850 record_clang_type, 1851 name, 1852 field_type, 1853 access, 1854 bitfield_bit_size, 1855 is_synthesized); 1856 } 1857 } 1858 } 1859 return false; 1860} 1861 1862bool 1863ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size) 1864{ 1865 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size); 1866} 1867 1868bool 1869ClangASTContext::FieldIsBitfield 1870( 1871 ASTContext *ast, 1872 FieldDecl* field, 1873 uint32_t& bitfield_bit_size 1874) 1875{ 1876 if (ast == NULL || field == NULL) 1877 return false; 1878 1879 if (field->isBitField()) 1880 { 1881 Expr* bit_width_expr = field->getBitWidth(); 1882 if (bit_width_expr) 1883 { 1884 llvm::APSInt bit_width_apsint; 1885 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) 1886 { 1887 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX); 1888 return true; 1889 } 1890 } 1891 } 1892 return false; 1893} 1894 1895bool 1896ClangASTContext::RecordHasFields (const RecordDecl *record_decl) 1897{ 1898 if (record_decl == NULL) 1899 return false; 1900 1901 if (!record_decl->field_empty()) 1902 return true; 1903 1904 // No fields, lets check this is a CXX record and check the base classes 1905 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1906 if (cxx_record_decl) 1907 { 1908 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1909 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1910 base_class != base_class_end; 1911 ++base_class) 1912 { 1913 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1914 if (RecordHasFields(base_class_decl)) 1915 return true; 1916 } 1917 } 1918 return false; 1919} 1920 1921void 1922ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities) 1923{ 1924 if (clang_type) 1925 { 1926 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 1927 1928 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr()); 1929 if (record_type) 1930 { 1931 RecordDecl *record_decl = record_type->getDecl(); 1932 if (record_decl) 1933 { 1934 uint32_t field_idx; 1935 RecordDecl::field_iterator field, field_end; 1936 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0; 1937 field != field_end; 1938 ++field, ++field_idx) 1939 { 1940 // If no accessibility was assigned, assign the correct one 1941 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none) 1942 field->setAccess ((AccessSpecifier)default_accessibility); 1943 } 1944 } 1945 } 1946 } 1947} 1948 1949#pragma mark C++ Base Classes 1950 1951CXXBaseSpecifier * 1952ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class) 1953{ 1954 if (base_class_type) 1955 return new CXXBaseSpecifier (SourceRange(), 1956 is_virtual, 1957 base_of_class, 1958 ConvertAccessTypeToAccessSpecifier (access), 1959 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)), 1960 SourceLocation()); 1961 return NULL; 1962} 1963 1964void 1965ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes) 1966{ 1967 for (unsigned i=0; i<num_base_classes; ++i) 1968 { 1969 delete base_classes[i]; 1970 base_classes[i] = NULL; 1971 } 1972} 1973 1974bool 1975ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes) 1976{ 1977 if (class_clang_type) 1978 { 1979 CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl(); 1980 if (cxx_record_decl) 1981 { 1982 cxx_record_decl->setBases(base_classes, num_base_classes); 1983 return true; 1984 } 1985 } 1986 return false; 1987} 1988#pragma mark Objective C Classes 1989 1990clang_type_t 1991ClangASTContext::CreateObjCClass 1992( 1993 const char *name, 1994 DeclContext *decl_ctx, 1995 bool isForwardDecl, 1996 bool isInternal 1997) 1998{ 1999 ASTContext *ast = getASTContext(); 2000 assert (ast != NULL); 2001 assert (name && name[0]); 2002 if (decl_ctx == NULL) 2003 decl_ctx = ast->getTranslationUnitDecl(); 2004 2005 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and 2006 // we will need to update this code. I was told to currently always use 2007 // the CXXRecordDecl class since we often don't know from debug information 2008 // if something is struct or a class, so we default to always use the more 2009 // complete definition just in case. 2010 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast, 2011 decl_ctx, 2012 SourceLocation(), 2013 &ast->Idents.get(name), 2014 SourceLocation(), 2015 isForwardDecl, 2016 isInternal); 2017 2018 return ast->getObjCInterfaceType(decl).getAsOpaquePtr(); 2019} 2020 2021bool 2022ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type) 2023{ 2024 if (class_opaque_type && super_opaque_type) 2025 { 2026 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 2027 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type)); 2028 const clang::Type *class_type = class_qual_type.getTypePtr(); 2029 const clang::Type *super_type = super_qual_type.getTypePtr(); 2030 if (class_type && super_type) 2031 { 2032 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 2033 const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type); 2034 if (objc_class_type && objc_super_type) 2035 { 2036 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2037 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface(); 2038 if (class_interface_decl && super_interface_decl) 2039 { 2040 class_interface_decl->setSuperClass(super_interface_decl); 2041 return true; 2042 } 2043 } 2044 } 2045 } 2046 return false; 2047} 2048 2049 2050bool 2051ClangASTContext::AddObjCClassIVar 2052( 2053 ASTContext *ast, 2054 clang_type_t class_opaque_type, 2055 const char *name, 2056 clang_type_t ivar_opaque_type, 2057 AccessType access, 2058 uint32_t bitfield_bit_size, 2059 bool is_synthesized 2060) 2061{ 2062 if (class_opaque_type == NULL || ivar_opaque_type == NULL) 2063 return false; 2064 2065 IdentifierTable *identifier_table = &ast->Idents; 2066 2067 assert (ast != NULL); 2068 assert (identifier_table != NULL); 2069 2070 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 2071 2072 const clang::Type *class_type = class_qual_type.getTypePtr(); 2073 if (class_type) 2074 { 2075 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 2076 2077 if (objc_class_type) 2078 { 2079 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2080 2081 if (class_interface_decl) 2082 { 2083 clang::Expr *bit_width = NULL; 2084 if (bitfield_bit_size != 0) 2085 { 2086 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size); 2087 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation()); 2088 } 2089 2090 ObjCIvarDecl *field = ObjCIvarDecl::Create (*ast, 2091 class_interface_decl, 2092 SourceLocation(), 2093 SourceLocation(), 2094 &identifier_table->get(name), // Identifier 2095 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type 2096 NULL, // TypeSourceInfo * 2097 ConvertAccessTypeToObjCIvarAccessControl (access), 2098 bit_width, 2099 is_synthesized); 2100 2101 if (field) 2102 { 2103 class_interface_decl->addDecl(field); 2104 2105#ifdef LLDB_CONFIGURATION_DEBUG 2106 VerifyDecl(field); 2107#endif 2108 2109 return true; 2110 } 2111 } 2112 } 2113 } 2114 return false; 2115} 2116 2117 2118bool 2119ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass) 2120{ 2121 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 2122 2123 const clang::Type *class_type = class_qual_type.getTypePtr(); 2124 if (class_type) 2125 { 2126 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 2127 2128 if (objc_class_type) 2129 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass); 2130 } 2131 return false; 2132} 2133 2134bool 2135ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass) 2136{ 2137 while (class_interface_decl) 2138 { 2139 if (class_interface_decl->ivar_size() > 0) 2140 return true; 2141 2142 if (check_superclass) 2143 class_interface_decl = class_interface_decl->getSuperClass(); 2144 else 2145 break; 2146 } 2147 return false; 2148} 2149 2150ObjCMethodDecl * 2151ClangASTContext::AddMethodToObjCObjectType 2152( 2153 ASTContext *ast, 2154 clang_type_t class_opaque_type, 2155 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]") 2156 clang_type_t method_opaque_type, 2157 lldb::AccessType access 2158) 2159{ 2160 if (class_opaque_type == NULL || method_opaque_type == NULL) 2161 return NULL; 2162 2163 IdentifierTable *identifier_table = &ast->Idents; 2164 2165 assert (ast != NULL); 2166 assert (identifier_table != NULL); 2167 2168 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 2169 2170 const clang::Type *class_type = class_qual_type.getTypePtr(); 2171 if (class_type == NULL) 2172 return NULL; 2173 2174 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 2175 2176 if (objc_class_type == NULL) 2177 return NULL; 2178 2179 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2180 2181 if (class_interface_decl == NULL) 2182 return NULL; 2183 2184 const char *selector_start = ::strchr (name, ' '); 2185 if (selector_start == NULL) 2186 return NULL; 2187 2188 selector_start++; 2189 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_')) 2190 return NULL; 2191 llvm::SmallVector<IdentifierInfo *, 12> selector_idents; 2192 2193 size_t len = 0; 2194 const char *start; 2195 //printf ("name = '%s'\n", name); 2196 2197 unsigned num_selectors_with_args = 0; 2198 for (start = selector_start; 2199 start && *start != '\0' && *start != ']'; 2200 start += len) 2201 { 2202 len = ::strcspn(start, ":]"); 2203 bool has_arg = (start[len] == ':'); 2204 if (has_arg) 2205 ++num_selectors_with_args; 2206 selector_idents.push_back (&identifier_table->get (StringRef (start, len))); 2207 if (has_arg) 2208 len += 1; 2209 } 2210 2211 2212 if (selector_idents.size() == 0) 2213 return 0; 2214 2215 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0, 2216 selector_idents.data()); 2217 2218 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type)); 2219 2220 // Populate the method decl with parameter decls 2221 const clang::Type *method_type(method_qual_type.getTypePtr()); 2222 2223 if (method_type == NULL) 2224 return NULL; 2225 2226 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type)); 2227 2228 if (!method_function_prototype) 2229 return NULL; 2230 2231 2232 bool is_variadic = false; 2233 bool is_synthesized = false; 2234 bool is_defined = false; 2235 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None; 2236 2237 const unsigned num_args = method_function_prototype->getNumArgs(); 2238 2239 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast, 2240 SourceLocation(), // beginLoc, 2241 SourceLocation(), // endLoc, 2242 method_selector, 2243 method_function_prototype->getResultType(), 2244 NULL, // TypeSourceInfo *ResultTInfo, 2245 GetDeclContextForType (class_opaque_type), 2246 name[0] == '-', 2247 is_variadic, 2248 is_synthesized, 2249 true, // is_implicitly_declared 2250 is_defined, 2251 imp_control, 2252 false /*has_related_result_type*/); 2253 2254 2255 if (objc_method_decl == NULL) 2256 return NULL; 2257 2258 if (num_args > 0) 2259 { 2260 llvm::SmallVector<ParmVarDecl *, 12> params; 2261 2262 for (int param_index = 0; param_index < num_args; ++param_index) 2263 { 2264 params.push_back (ParmVarDecl::Create (*ast, 2265 objc_method_decl, 2266 SourceLocation(), 2267 SourceLocation(), 2268 NULL, // anonymous 2269 method_function_prototype->getArgType(param_index), 2270 NULL, 2271 SC_Auto, 2272 SC_Auto, 2273 NULL)); 2274 } 2275 2276 objc_method_decl->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>()); 2277 } 2278 2279 class_interface_decl->addDecl (objc_method_decl); 2280 2281#ifdef LLDB_CONFIGURATION_DEBUG 2282 VerifyDecl(objc_method_decl); 2283#endif 2284 2285 return objc_method_decl; 2286} 2287 2288 2289uint32_t 2290ClangASTContext::GetTypeInfo 2291( 2292 clang_type_t clang_type, 2293 clang::ASTContext *ast, 2294 clang_type_t *pointee_or_element_clang_type 2295) 2296{ 2297 if (clang_type == NULL) 2298 return 0; 2299 2300 if (pointee_or_element_clang_type) 2301 *pointee_or_element_clang_type = NULL; 2302 2303 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2304 2305 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2306 switch (type_class) 2307 { 2308 case clang::Type::Builtin: 2309 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 2310 { 2311 case clang::BuiltinType::ObjCId: 2312 case clang::BuiltinType::ObjCClass: 2313 if (ast && pointee_or_element_clang_type) 2314 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr(); 2315 return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue; 2316 break; 2317 case clang::BuiltinType::Bool: 2318 case clang::BuiltinType::Char_U: 2319 case clang::BuiltinType::UChar: 2320 case clang::BuiltinType::WChar_U: 2321 case clang::BuiltinType::Char16: 2322 case clang::BuiltinType::Char32: 2323 case clang::BuiltinType::UShort: 2324 case clang::BuiltinType::UInt: 2325 case clang::BuiltinType::ULong: 2326 case clang::BuiltinType::ULongLong: 2327 case clang::BuiltinType::UInt128: 2328 case clang::BuiltinType::Char_S: 2329 case clang::BuiltinType::SChar: 2330 case clang::BuiltinType::WChar_S: 2331 case clang::BuiltinType::Short: 2332 case clang::BuiltinType::Int: 2333 case clang::BuiltinType::Long: 2334 case clang::BuiltinType::LongLong: 2335 case clang::BuiltinType::Int128: 2336 case clang::BuiltinType::Float: 2337 case clang::BuiltinType::Double: 2338 case clang::BuiltinType::LongDouble: 2339 return eTypeIsBuiltIn | eTypeHasValue | eTypeIsScalar; 2340 default: 2341 break; 2342 } 2343 return eTypeIsBuiltIn | eTypeHasValue; 2344 2345 case clang::Type::BlockPointer: 2346 if (pointee_or_element_clang_type) 2347 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr(); 2348 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock; 2349 2350 case clang::Type::Complex: return eTypeIsBuiltIn | eTypeHasValue; 2351 2352 case clang::Type::ConstantArray: 2353 case clang::Type::DependentSizedArray: 2354 case clang::Type::IncompleteArray: 2355 case clang::Type::VariableArray: 2356 if (pointee_or_element_clang_type) 2357 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr(); 2358 return eTypeHasChildren | eTypeIsArray; 2359 2360 case clang::Type::DependentName: return 0; 2361 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector; 2362 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate; 2363 case clang::Type::Decltype: return 0; 2364 2365 case clang::Type::Enum: 2366 if (pointee_or_element_clang_type) 2367 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr(); 2368 return eTypeIsEnumeration | eTypeHasValue; 2369 2370 case clang::Type::Elaborated: 2371 return ClangASTContext::GetTypeInfo (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), 2372 ast, 2373 pointee_or_element_clang_type); 2374 case clang::Type::ExtVector: return eTypeHasChildren | eTypeIsVector; 2375 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue; 2376 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue; 2377 case clang::Type::InjectedClassName: return 0; 2378 2379 case clang::Type::LValueReference: 2380 case clang::Type::RValueReference: 2381 if (pointee_or_element_clang_type) 2382 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(); 2383 return eTypeHasChildren | eTypeIsReference | eTypeHasValue; 2384 2385 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue; 2386 2387 case clang::Type::ObjCObjectPointer: 2388 if (pointee_or_element_clang_type) 2389 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr(); 2390 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue; 2391 2392 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; 2393 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; 2394 2395 case clang::Type::Pointer: 2396 if (pointee_or_element_clang_type) 2397 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr(); 2398 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue; 2399 2400 case clang::Type::Record: 2401 if (qual_type->getAsCXXRecordDecl()) 2402 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus; 2403 else 2404 return eTypeHasChildren | eTypeIsStructUnion; 2405 break; 2406 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate; 2407 case clang::Type::TemplateTypeParm: return eTypeIsTemplate; 2408 case clang::Type::TemplateSpecialization: return eTypeIsTemplate; 2409 2410 case clang::Type::Typedef: 2411 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 2412 ast, 2413 pointee_or_element_clang_type); 2414 2415 case clang::Type::TypeOfExpr: return 0; 2416 case clang::Type::TypeOf: return 0; 2417 case clang::Type::UnresolvedUsing: return 0; 2418 case clang::Type::Vector: return eTypeHasChildren | eTypeIsVector; 2419 default: return 0; 2420 } 2421 return 0; 2422} 2423 2424 2425#pragma mark Aggregate Types 2426 2427bool 2428ClangASTContext::IsAggregateType (clang_type_t clang_type) 2429{ 2430 if (clang_type == NULL) 2431 return false; 2432 2433 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2434 2435 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2436 switch (type_class) 2437 { 2438 case clang::Type::IncompleteArray: 2439 case clang::Type::VariableArray: 2440 case clang::Type::ConstantArray: 2441 case clang::Type::ExtVector: 2442 case clang::Type::Vector: 2443 case clang::Type::Record: 2444 case clang::Type::ObjCObject: 2445 case clang::Type::ObjCInterface: 2446 return true; 2447 case clang::Type::Elaborated: 2448 return ClangASTContext::IsAggregateType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 2449 case clang::Type::Typedef: 2450 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 2451 2452 default: 2453 break; 2454 } 2455 // The clang type does have a value 2456 return false; 2457} 2458 2459uint32_t 2460ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes) 2461{ 2462 if (clang_type == NULL) 2463 return 0; 2464 2465 uint32_t num_children = 0; 2466 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2467 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2468 switch (type_class) 2469 { 2470 case clang::Type::Builtin: 2471 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 2472 { 2473 case clang::BuiltinType::ObjCId: // child is Class 2474 case clang::BuiltinType::ObjCClass: // child is Class 2475 num_children = 1; 2476 break; 2477 2478 default: 2479 break; 2480 } 2481 break; 2482 2483 case clang::Type::Complex: return 0; 2484 2485 case clang::Type::Record: 2486 if (GetCompleteQualType (ast, qual_type)) 2487 { 2488 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 2489 const RecordDecl *record_decl = record_type->getDecl(); 2490 assert(record_decl); 2491 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 2492 if (cxx_record_decl) 2493 { 2494 if (omit_empty_base_classes) 2495 { 2496 // Check each base classes to see if it or any of its 2497 // base classes contain any fields. This can help 2498 // limit the noise in variable views by not having to 2499 // show base classes that contain no members. 2500 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2501 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2502 base_class != base_class_end; 2503 ++base_class) 2504 { 2505 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2506 2507 // Skip empty base classes 2508 if (RecordHasFields(base_class_decl) == false) 2509 continue; 2510 2511 num_children++; 2512 } 2513 } 2514 else 2515 { 2516 // Include all base classes 2517 num_children += cxx_record_decl->getNumBases(); 2518 } 2519 2520 } 2521 RecordDecl::field_iterator field, field_end; 2522 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field) 2523 ++num_children; 2524 } 2525 break; 2526 2527 case clang::Type::ObjCObject: 2528 case clang::Type::ObjCInterface: 2529 if (GetCompleteQualType (ast, qual_type)) 2530 { 2531 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 2532 assert (objc_class_type); 2533 if (objc_class_type) 2534 { 2535 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2536 2537 if (class_interface_decl) 2538 { 2539 2540 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 2541 if (superclass_interface_decl) 2542 { 2543 if (omit_empty_base_classes) 2544 { 2545 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true)) 2546 ++num_children; 2547 } 2548 else 2549 ++num_children; 2550 } 2551 2552 num_children += class_interface_decl->ivar_size(); 2553 } 2554 } 2555 } 2556 break; 2557 2558 case clang::Type::ObjCObjectPointer: 2559 { 2560 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr()); 2561 QualType pointee_type = pointer_type->getPointeeType(); 2562 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast, 2563 pointee_type.getAsOpaquePtr(), 2564 omit_empty_base_classes); 2565 // If this type points to a simple type, then it has 1 child 2566 if (num_pointee_children == 0) 2567 num_children = 1; 2568 else 2569 num_children = num_pointee_children; 2570 } 2571 break; 2572 2573 case clang::Type::ConstantArray: 2574 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue(); 2575 break; 2576 2577 case clang::Type::Pointer: 2578 { 2579 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 2580 QualType pointee_type (pointer_type->getPointeeType()); 2581 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast, 2582 pointee_type.getAsOpaquePtr(), 2583 omit_empty_base_classes); 2584 if (num_pointee_children == 0) 2585 { 2586 // We have a pointer to a pointee type that claims it has no children. 2587 // We will want to look at 2588 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr()); 2589 } 2590 else 2591 num_children = num_pointee_children; 2592 } 2593 break; 2594 2595 case clang::Type::LValueReference: 2596 case clang::Type::RValueReference: 2597 { 2598 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 2599 QualType pointee_type = reference_type->getPointeeType(); 2600 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast, 2601 pointee_type.getAsOpaquePtr(), 2602 omit_empty_base_classes); 2603 // If this type points to a simple type, then it has 1 child 2604 if (num_pointee_children == 0) 2605 num_children = 1; 2606 else 2607 num_children = num_pointee_children; 2608 } 2609 break; 2610 2611 2612 case clang::Type::Typedef: 2613 num_children = ClangASTContext::GetNumChildren (ast, 2614 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 2615 omit_empty_base_classes); 2616 break; 2617 2618 case clang::Type::Elaborated: 2619 num_children = ClangASTContext::GetNumChildren (ast, 2620 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), 2621 omit_empty_base_classes); 2622 break; 2623 2624 default: 2625 break; 2626 } 2627 return num_children; 2628} 2629 2630uint32_t 2631ClangASTContext::GetNumDirectBaseClasses (clang::ASTContext *ast, clang_type_t clang_type) 2632{ 2633 if (clang_type == NULL) 2634 return 0; 2635 2636 uint32_t count = 0; 2637 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2638 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2639 switch (type_class) 2640 { 2641 case clang::Type::Record: 2642 if (GetCompleteQualType (ast, qual_type)) 2643 { 2644 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 2645 if (cxx_record_decl) 2646 count = cxx_record_decl->getNumBases(); 2647 } 2648 break; 2649 2650 case clang::Type::ObjCObject: 2651 case clang::Type::ObjCInterface: 2652 if (GetCompleteQualType (ast, qual_type)) 2653 { 2654 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType(); 2655 if (objc_class_type) 2656 { 2657 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2658 2659 if (class_interface_decl && class_interface_decl->getSuperClass()) 2660 count = 1; 2661 } 2662 } 2663 break; 2664 2665 2666 case clang::Type::Typedef: 2667 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 2668 break; 2669 2670 case clang::Type::Elaborated: 2671 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 2672 break; 2673 2674 default: 2675 break; 2676 } 2677 return count; 2678} 2679 2680uint32_t 2681ClangASTContext::GetNumVirtualBaseClasses (clang::ASTContext *ast, 2682 clang_type_t clang_type) 2683{ 2684 if (clang_type == NULL) 2685 return 0; 2686 2687 uint32_t count = 0; 2688 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2689 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2690 switch (type_class) 2691 { 2692 case clang::Type::Record: 2693 if (GetCompleteQualType (ast, qual_type)) 2694 { 2695 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 2696 if (cxx_record_decl) 2697 count = cxx_record_decl->getNumVBases(); 2698 } 2699 break; 2700 2701 case clang::Type::Typedef: 2702 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 2703 break; 2704 2705 case clang::Type::Elaborated: 2706 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 2707 break; 2708 2709 default: 2710 break; 2711 } 2712 return count; 2713} 2714 2715uint32_t 2716ClangASTContext::GetNumFields (clang::ASTContext *ast, clang_type_t clang_type) 2717{ 2718 if (clang_type == NULL) 2719 return 0; 2720 2721 uint32_t count = 0; 2722 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2723 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2724 switch (type_class) 2725 { 2726 case clang::Type::Record: 2727 if (GetCompleteQualType (ast, qual_type)) 2728 { 2729 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr()); 2730 if (record_type) 2731 { 2732 RecordDecl *record_decl = record_type->getDecl(); 2733 if (record_decl) 2734 { 2735 uint32_t field_idx = 0; 2736 RecordDecl::field_iterator field, field_end; 2737 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field) 2738 ++field_idx; 2739 count = field_idx; 2740 } 2741 } 2742 } 2743 break; 2744 2745 case clang::Type::Typedef: 2746 count = ClangASTContext::GetNumFields (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 2747 break; 2748 2749 case clang::Type::Elaborated: 2750 count = ClangASTContext::GetNumFields (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 2751 break; 2752 2753 default: 2754 break; 2755 } 2756 return count; 2757} 2758 2759clang_type_t 2760ClangASTContext::GetDirectBaseClassAtIndex (clang::ASTContext *ast, 2761 clang_type_t clang_type, 2762 uint32_t idx, 2763 uint32_t *byte_offset_ptr) 2764{ 2765 if (clang_type == NULL) 2766 return 0; 2767 2768 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2769 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2770 switch (type_class) 2771 { 2772 case clang::Type::Record: 2773 if (GetCompleteQualType (ast, qual_type)) 2774 { 2775 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 2776 if (cxx_record_decl) 2777 { 2778 uint32_t curr_idx = 0; 2779 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2780 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2781 base_class != base_class_end; 2782 ++base_class, ++curr_idx) 2783 { 2784 if (curr_idx == idx) 2785 { 2786 if (byte_offset_ptr) 2787 { 2788 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl); 2789 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2790// if (base_class->isVirtual()) 2791// *byte_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8; 2792// else 2793 *byte_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8; 2794 } 2795 return base_class->getType().getAsOpaquePtr(); 2796 } 2797 } 2798 } 2799 } 2800 break; 2801 2802 case clang::Type::ObjCObject: 2803 case clang::Type::ObjCInterface: 2804 if (idx == 0 && GetCompleteQualType (ast, qual_type)) 2805 { 2806 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType(); 2807 if (objc_class_type) 2808 { 2809 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2810 2811 if (class_interface_decl) 2812 { 2813 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 2814 if (superclass_interface_decl) 2815 { 2816 if (byte_offset_ptr) 2817 *byte_offset_ptr = 0; 2818 return ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(); 2819 } 2820 } 2821 } 2822 } 2823 break; 2824 2825 2826 case clang::Type::Typedef: 2827 return ClangASTContext::GetDirectBaseClassAtIndex (ast, 2828 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 2829 idx, 2830 byte_offset_ptr); 2831 2832 case clang::Type::Elaborated: 2833 return ClangASTContext::GetDirectBaseClassAtIndex (ast, 2834 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), 2835 idx, 2836 byte_offset_ptr); 2837 2838 default: 2839 break; 2840 } 2841 return NULL; 2842} 2843 2844clang_type_t 2845ClangASTContext::GetVirtualBaseClassAtIndex (clang::ASTContext *ast, 2846 clang_type_t clang_type, 2847 uint32_t idx, 2848 uint32_t *byte_offset_ptr) 2849{ 2850 if (clang_type == NULL) 2851 return 0; 2852 2853 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2854 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2855 switch (type_class) 2856 { 2857 case clang::Type::Record: 2858 if (GetCompleteQualType (ast, qual_type)) 2859 { 2860 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 2861 if (cxx_record_decl) 2862 { 2863 uint32_t curr_idx = 0; 2864 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2865 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end(); 2866 base_class != base_class_end; 2867 ++base_class, ++curr_idx) 2868 { 2869 if (curr_idx == idx) 2870 { 2871 if (byte_offset_ptr) 2872 { 2873 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl); 2874 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2875 *byte_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8; 2876 2877 } 2878 return base_class->getType().getAsOpaquePtr(); 2879 } 2880 } 2881 } 2882 } 2883 break; 2884 2885 case clang::Type::Typedef: 2886 return ClangASTContext::GetVirtualBaseClassAtIndex (ast, 2887 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 2888 idx, 2889 byte_offset_ptr); 2890 2891 case clang::Type::Elaborated: 2892 return ClangASTContext::GetVirtualBaseClassAtIndex (ast, 2893 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), 2894 idx, 2895 byte_offset_ptr); 2896 2897 default: 2898 break; 2899 } 2900 return NULL; 2901} 2902 2903clang_type_t 2904ClangASTContext::GetFieldAtIndex (clang::ASTContext *ast, 2905 clang_type_t clang_type, 2906 uint32_t idx, 2907 std::string& name, 2908 uint32_t *byte_offset_ptr) 2909{ 2910 if (clang_type == NULL) 2911 return 0; 2912 2913 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2914 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2915 switch (type_class) 2916 { 2917 case clang::Type::Record: 2918 if (GetCompleteQualType (ast, qual_type)) 2919 { 2920 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 2921 const RecordDecl *record_decl = record_type->getDecl(); 2922 uint32_t field_idx = 0; 2923 RecordDecl::field_iterator field, field_end; 2924 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx) 2925 { 2926 if (idx == field_idx) 2927 { 2928 // Print the member type if requested 2929 // Print the member name and equal sign 2930 name.assign(field->getNameAsString()); 2931 2932 // Figure out the type byte size (field_type_info.first) and 2933 // alignment (field_type_info.second) from the AST context. 2934 if (byte_offset_ptr) 2935 { 2936 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl); 2937 *byte_offset_ptr = (record_layout.getFieldOffset (field_idx) + 7) / 8; 2938 } 2939 2940 return field->getType().getAsOpaquePtr(); 2941 } 2942 } 2943 } 2944 break; 2945 2946 case clang::Type::ObjCObject: 2947 case clang::Type::ObjCInterface: 2948 if (GetCompleteQualType (ast, qual_type)) 2949 { 2950 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 2951 assert (objc_class_type); 2952 if (objc_class_type) 2953 { 2954 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2955 2956 if (class_interface_decl) 2957 { 2958 if (idx < (class_interface_decl->ivar_size())) 2959 { 2960 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 2961 uint32_t ivar_idx = 0; 2962 2963 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx) 2964 { 2965 if (ivar_idx == idx) 2966 { 2967 const ObjCIvarDecl* ivar_decl = *ivar_pos; 2968 2969 QualType ivar_qual_type(ivar_decl->getType()); 2970 2971 name.assign(ivar_decl->getNameAsString()); 2972 2973 if (byte_offset_ptr) 2974 { 2975 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl); 2976 *byte_offset_ptr = (interface_layout.getFieldOffset (ivar_idx) + 7)/8; 2977 } 2978 2979 return ivar_qual_type.getAsOpaquePtr(); 2980 } 2981 } 2982 } 2983 } 2984 } 2985 } 2986 break; 2987 2988 2989 case clang::Type::Typedef: 2990 return ClangASTContext::GetFieldAtIndex (ast, 2991 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 2992 idx, 2993 name, 2994 byte_offset_ptr); 2995 2996 case clang::Type::Elaborated: 2997 return ClangASTContext::GetFieldAtIndex (ast, 2998 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), 2999 idx, 3000 name, 3001 byte_offset_ptr); 3002 3003 default: 3004 break; 3005 } 3006 return NULL; 3007} 3008 3009 3010// If a pointer to a pointee type (the clang_type arg) says that it has no 3011// children, then we either need to trust it, or override it and return a 3012// different result. For example, an "int *" has one child that is an integer, 3013// but a function pointer doesn't have any children. Likewise if a Record type 3014// claims it has no children, then there really is nothing to show. 3015uint32_t 3016ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type) 3017{ 3018 if (clang_type == NULL) 3019 return 0; 3020 3021 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 3022 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3023 switch (type_class) 3024 { 3025 case clang::Type::Builtin: 3026 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 3027 { 3028 case clang::BuiltinType::UnknownAny: 3029 case clang::BuiltinType::Void: 3030 case clang::BuiltinType::NullPtr: 3031 return 0; 3032 case clang::BuiltinType::Bool: 3033 case clang::BuiltinType::Char_U: 3034 case clang::BuiltinType::UChar: 3035 case clang::BuiltinType::WChar_U: 3036 case clang::BuiltinType::Char16: 3037 case clang::BuiltinType::Char32: 3038 case clang::BuiltinType::UShort: 3039 case clang::BuiltinType::UInt: 3040 case clang::BuiltinType::ULong: 3041 case clang::BuiltinType::ULongLong: 3042 case clang::BuiltinType::UInt128: 3043 case clang::BuiltinType::Char_S: 3044 case clang::BuiltinType::SChar: 3045 case clang::BuiltinType::WChar_S: 3046 case clang::BuiltinType::Short: 3047 case clang::BuiltinType::Int: 3048 case clang::BuiltinType::Long: 3049 case clang::BuiltinType::LongLong: 3050 case clang::BuiltinType::Int128: 3051 case clang::BuiltinType::Float: 3052 case clang::BuiltinType::Double: 3053 case clang::BuiltinType::LongDouble: 3054 case clang::BuiltinType::Dependent: 3055 case clang::BuiltinType::Overload: 3056 case clang::BuiltinType::ObjCId: 3057 case clang::BuiltinType::ObjCClass: 3058 case clang::BuiltinType::ObjCSel: 3059 case clang::BuiltinType::BoundMember: 3060 case clang::BuiltinType::Half: 3061 case clang::BuiltinType::ARCUnbridgedCast: 3062 case clang::BuiltinType::PseudoObject: 3063 return 1; 3064 } 3065 break; 3066 3067 case clang::Type::Complex: return 1; 3068 case clang::Type::Pointer: return 1; 3069 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them 3070 case clang::Type::LValueReference: return 1; 3071 case clang::Type::RValueReference: return 1; 3072 case clang::Type::MemberPointer: return 0; 3073 case clang::Type::ConstantArray: return 0; 3074 case clang::Type::IncompleteArray: return 0; 3075 case clang::Type::VariableArray: return 0; 3076 case clang::Type::DependentSizedArray: return 0; 3077 case clang::Type::DependentSizedExtVector: return 0; 3078 case clang::Type::Vector: return 0; 3079 case clang::Type::ExtVector: return 0; 3080 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children... 3081 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children... 3082 case clang::Type::UnresolvedUsing: return 0; 3083 case clang::Type::Paren: return 0; 3084 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 3085 case clang::Type::Elaborated: return ClangASTContext::GetNumPointeeChildren (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 3086 case clang::Type::TypeOfExpr: return 0; 3087 case clang::Type::TypeOf: return 0; 3088 case clang::Type::Decltype: return 0; 3089 case clang::Type::Record: return 0; 3090 case clang::Type::Enum: return 1; 3091 case clang::Type::TemplateTypeParm: return 1; 3092 case clang::Type::SubstTemplateTypeParm: return 1; 3093 case clang::Type::TemplateSpecialization: return 1; 3094 case clang::Type::InjectedClassName: return 0; 3095 case clang::Type::DependentName: return 1; 3096 case clang::Type::DependentTemplateSpecialization: return 1; 3097 case clang::Type::ObjCObject: return 0; 3098 case clang::Type::ObjCInterface: return 0; 3099 case clang::Type::ObjCObjectPointer: return 1; 3100 default: 3101 break; 3102 } 3103 return 0; 3104} 3105 3106clang_type_t 3107ClangASTContext::GetChildClangTypeAtIndex 3108( 3109 ExecutionContext *exe_ctx, 3110 const char *parent_name, 3111 clang_type_t parent_clang_type, 3112 uint32_t idx, 3113 bool transparent_pointers, 3114 bool omit_empty_base_classes, 3115 bool ignore_array_bounds, 3116 std::string& child_name, 3117 uint32_t &child_byte_size, 3118 int32_t &child_byte_offset, 3119 uint32_t &child_bitfield_bit_size, 3120 uint32_t &child_bitfield_bit_offset, 3121 bool &child_is_base_class, 3122 bool &child_is_deref_of_parent 3123) 3124{ 3125 if (parent_clang_type) 3126 3127 return GetChildClangTypeAtIndex (exe_ctx, 3128 getASTContext(), 3129 parent_name, 3130 parent_clang_type, 3131 idx, 3132 transparent_pointers, 3133 omit_empty_base_classes, 3134 ignore_array_bounds, 3135 child_name, 3136 child_byte_size, 3137 child_byte_offset, 3138 child_bitfield_bit_size, 3139 child_bitfield_bit_offset, 3140 child_is_base_class, 3141 child_is_deref_of_parent); 3142 return NULL; 3143} 3144 3145clang_type_t 3146ClangASTContext::GetChildClangTypeAtIndex 3147( 3148 ExecutionContext *exe_ctx, 3149 ASTContext *ast, 3150 const char *parent_name, 3151 clang_type_t parent_clang_type, 3152 uint32_t idx, 3153 bool transparent_pointers, 3154 bool omit_empty_base_classes, 3155 bool ignore_array_bounds, 3156 std::string& child_name, 3157 uint32_t &child_byte_size, 3158 int32_t &child_byte_offset, 3159 uint32_t &child_bitfield_bit_size, 3160 uint32_t &child_bitfield_bit_offset, 3161 bool &child_is_base_class, 3162 bool &child_is_deref_of_parent 3163) 3164{ 3165 if (parent_clang_type == NULL) 3166 return NULL; 3167 3168 if (idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes)) 3169 { 3170 uint32_t bit_offset; 3171 child_bitfield_bit_size = 0; 3172 child_bitfield_bit_offset = 0; 3173 child_is_base_class = false; 3174 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type)); 3175 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass(); 3176 switch (parent_type_class) 3177 { 3178 case clang::Type::Builtin: 3179 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind()) 3180 { 3181 case clang::BuiltinType::ObjCId: 3182 case clang::BuiltinType::ObjCClass: 3183 child_name = "isa"; 3184 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT; 3185 return ast->ObjCBuiltinClassTy.getAsOpaquePtr(); 3186 3187 default: 3188 break; 3189 } 3190 break; 3191 3192 case clang::Type::Record: 3193 if (GetCompleteQualType (ast, parent_qual_type)) 3194 { 3195 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr()); 3196 const RecordDecl *record_decl = record_type->getDecl(); 3197 assert(record_decl); 3198 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl); 3199 uint32_t child_idx = 0; 3200 3201 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 3202 if (cxx_record_decl) 3203 { 3204 // We might have base classes to print out first 3205 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 3206 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 3207 base_class != base_class_end; 3208 ++base_class) 3209 { 3210 const CXXRecordDecl *base_class_decl = NULL; 3211 3212 // Skip empty base classes 3213 if (omit_empty_base_classes) 3214 { 3215 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 3216 if (RecordHasFields(base_class_decl) == false) 3217 continue; 3218 } 3219 3220 if (idx == child_idx) 3221 { 3222 if (base_class_decl == NULL) 3223 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 3224 3225 3226 if (base_class->isVirtual()) 3227 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8; 3228 else 3229 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8; 3230 3231 // Base classes should be a multiple of 8 bits in size 3232 child_byte_offset = bit_offset/8; 3233 3234 child_name = ClangASTType::GetTypeNameForQualType(base_class->getType()); 3235 3236 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType()); 3237 3238 // Base classes bit sizes should be a multiple of 8 bits in size 3239 assert (clang_type_info_bit_size % 8 == 0); 3240 child_byte_size = clang_type_info_bit_size / 8; 3241 child_is_base_class = true; 3242 return base_class->getType().getAsOpaquePtr(); 3243 } 3244 // We don't increment the child index in the for loop since we might 3245 // be skipping empty base classes 3246 ++child_idx; 3247 } 3248 } 3249 // Make sure index is in range... 3250 uint32_t field_idx = 0; 3251 RecordDecl::field_iterator field, field_end; 3252 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx) 3253 { 3254 if (idx == child_idx) 3255 { 3256 // Print the member type if requested 3257 // Print the member name and equal sign 3258 child_name.assign(field->getNameAsString().c_str()); 3259 3260 // Figure out the type byte size (field_type_info.first) and 3261 // alignment (field_type_info.second) from the AST context. 3262 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType()); 3263 assert(field_idx < record_layout.getFieldCount()); 3264 3265 child_byte_size = field_type_info.first / 8; 3266 3267 // Figure out the field offset within the current struct/union/class type 3268 bit_offset = record_layout.getFieldOffset (field_idx); 3269 child_byte_offset = bit_offset / 8; 3270 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size)) 3271 child_bitfield_bit_offset = bit_offset % 8; 3272 3273 return field->getType().getAsOpaquePtr(); 3274 } 3275 } 3276 } 3277 break; 3278 3279 case clang::Type::ObjCObject: 3280 case clang::Type::ObjCInterface: 3281 if (GetCompleteQualType (ast, parent_qual_type)) 3282 { 3283 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr()); 3284 assert (objc_class_type); 3285 if (objc_class_type) 3286 { 3287 uint32_t child_idx = 0; 3288 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 3289 3290 if (class_interface_decl) 3291 { 3292 3293 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl); 3294 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 3295 if (superclass_interface_decl) 3296 { 3297 if (omit_empty_base_classes) 3298 { 3299 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0) 3300 { 3301 if (idx == 0) 3302 { 3303 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl)); 3304 3305 3306 child_name.assign(superclass_interface_decl->getNameAsString().c_str()); 3307 3308 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr()); 3309 3310 child_byte_size = ivar_type_info.first / 8; 3311 child_byte_offset = 0; 3312 child_is_base_class = true; 3313 3314 return ivar_qual_type.getAsOpaquePtr(); 3315 } 3316 3317 ++child_idx; 3318 } 3319 } 3320 else 3321 ++child_idx; 3322 } 3323 3324 const uint32_t superclass_idx = child_idx; 3325 3326 if (idx < (child_idx + class_interface_decl->ivar_size())) 3327 { 3328 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 3329 3330 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos) 3331 { 3332 if (child_idx == idx) 3333 { 3334 const ObjCIvarDecl* ivar_decl = *ivar_pos; 3335 3336 QualType ivar_qual_type(ivar_decl->getType()); 3337 3338 child_name.assign(ivar_decl->getNameAsString().c_str()); 3339 3340 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr()); 3341 3342 child_byte_size = ivar_type_info.first / 8; 3343 3344 // Figure out the field offset within the current struct/union/class type 3345 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since 3346 // that doesn't account for the space taken up by unbacked properties, or from 3347 // the changing size of base classes that are newer than this class. 3348 // So if we have a process around that we can ask about this object, do so. 3349 child_byte_offset = LLDB_INVALID_IVAR_OFFSET; 3350 Process *process = NULL; 3351 if (exe_ctx) 3352 process = exe_ctx->GetProcessPtr(); 3353 if (process) 3354 { 3355 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime(); 3356 if (objc_runtime != NULL) 3357 { 3358 ClangASTType parent_ast_type (ast, parent_qual_type.getAsOpaquePtr()); 3359 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str()); 3360 } 3361 } 3362 3363 if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET) 3364 { 3365 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx); 3366 child_byte_offset = bit_offset / 8; 3367 } 3368 3369 return ivar_qual_type.getAsOpaquePtr(); 3370 } 3371 ++child_idx; 3372 } 3373 } 3374 } 3375 } 3376 } 3377 break; 3378 3379 case clang::Type::ObjCObjectPointer: 3380 { 3381 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr()); 3382 QualType pointee_type = pointer_type->getPointeeType(); 3383 3384 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3385 { 3386 child_is_deref_of_parent = false; 3387 bool tmp_child_is_deref_of_parent = false; 3388 return GetChildClangTypeAtIndex (exe_ctx, 3389 ast, 3390 parent_name, 3391 pointer_type->getPointeeType().getAsOpaquePtr(), 3392 idx, 3393 transparent_pointers, 3394 omit_empty_base_classes, 3395 ignore_array_bounds, 3396 child_name, 3397 child_byte_size, 3398 child_byte_offset, 3399 child_bitfield_bit_size, 3400 child_bitfield_bit_offset, 3401 child_is_base_class, 3402 tmp_child_is_deref_of_parent); 3403 } 3404 else 3405 { 3406 child_is_deref_of_parent = true; 3407 if (parent_name) 3408 { 3409 child_name.assign(1, '*'); 3410 child_name += parent_name; 3411 } 3412 3413 // We have a pointer to an simple type 3414 if (idx == 0) 3415 { 3416 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 3417 assert(clang_type_info.first % 8 == 0); 3418 child_byte_size = clang_type_info.first / 8; 3419 child_byte_offset = 0; 3420 return pointee_type.getAsOpaquePtr(); 3421 } 3422 } 3423 } 3424 break; 3425 3426 case clang::Type::ConstantArray: 3427 { 3428 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 3429 const uint64_t element_count = array->getSize().getLimitedValue(); 3430 3431 if (ignore_array_bounds || idx < element_count) 3432 { 3433 if (GetCompleteQualType (ast, array->getElementType())) 3434 { 3435 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType()); 3436 3437 char element_name[64]; 3438 ::snprintf (element_name, sizeof (element_name), "[%u]", idx); 3439 3440 child_name.assign(element_name); 3441 assert(field_type_info.first % 8 == 0); 3442 child_byte_size = field_type_info.first / 8; 3443 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; 3444 return array->getElementType().getAsOpaquePtr(); 3445 } 3446 } 3447 } 3448 break; 3449 3450 case clang::Type::Pointer: 3451 { 3452 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr()); 3453 QualType pointee_type = pointer_type->getPointeeType(); 3454 3455 // Don't dereference "void *" pointers 3456 if (pointee_type->isVoidType()) 3457 return NULL; 3458 3459 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3460 { 3461 child_is_deref_of_parent = false; 3462 bool tmp_child_is_deref_of_parent = false; 3463 return GetChildClangTypeAtIndex (exe_ctx, 3464 ast, 3465 parent_name, 3466 pointer_type->getPointeeType().getAsOpaquePtr(), 3467 idx, 3468 transparent_pointers, 3469 omit_empty_base_classes, 3470 ignore_array_bounds, 3471 child_name, 3472 child_byte_size, 3473 child_byte_offset, 3474 child_bitfield_bit_size, 3475 child_bitfield_bit_offset, 3476 child_is_base_class, 3477 tmp_child_is_deref_of_parent); 3478 } 3479 else 3480 { 3481 child_is_deref_of_parent = true; 3482 3483 if (parent_name) 3484 { 3485 child_name.assign(1, '*'); 3486 child_name += parent_name; 3487 } 3488 3489 // We have a pointer to an simple type 3490 if (idx == 0) 3491 { 3492 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 3493 assert(clang_type_info.first % 8 == 0); 3494 child_byte_size = clang_type_info.first / 8; 3495 child_byte_offset = 0; 3496 return pointee_type.getAsOpaquePtr(); 3497 } 3498 } 3499 } 3500 break; 3501 3502 case clang::Type::LValueReference: 3503 case clang::Type::RValueReference: 3504 { 3505 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr()); 3506 QualType pointee_type(reference_type->getPointeeType()); 3507 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr(); 3508 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type)) 3509 { 3510 child_is_deref_of_parent = false; 3511 bool tmp_child_is_deref_of_parent = false; 3512 return GetChildClangTypeAtIndex (exe_ctx, 3513 ast, 3514 parent_name, 3515 pointee_clang_type, 3516 idx, 3517 transparent_pointers, 3518 omit_empty_base_classes, 3519 ignore_array_bounds, 3520 child_name, 3521 child_byte_size, 3522 child_byte_offset, 3523 child_bitfield_bit_size, 3524 child_bitfield_bit_offset, 3525 child_is_base_class, 3526 tmp_child_is_deref_of_parent); 3527 } 3528 else 3529 { 3530 if (parent_name) 3531 { 3532 child_name.assign(1, '&'); 3533 child_name += parent_name; 3534 } 3535 3536 // We have a pointer to an simple type 3537 if (idx == 0) 3538 { 3539 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 3540 assert(clang_type_info.first % 8 == 0); 3541 child_byte_size = clang_type_info.first / 8; 3542 child_byte_offset = 0; 3543 return pointee_type.getAsOpaquePtr(); 3544 } 3545 } 3546 } 3547 break; 3548 3549 case clang::Type::Typedef: 3550 return GetChildClangTypeAtIndex (exe_ctx, 3551 ast, 3552 parent_name, 3553 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 3554 idx, 3555 transparent_pointers, 3556 omit_empty_base_classes, 3557 ignore_array_bounds, 3558 child_name, 3559 child_byte_size, 3560 child_byte_offset, 3561 child_bitfield_bit_size, 3562 child_bitfield_bit_offset, 3563 child_is_base_class, 3564 child_is_deref_of_parent); 3565 break; 3566 3567 case clang::Type::Elaborated: 3568 return GetChildClangTypeAtIndex (exe_ctx, 3569 ast, 3570 parent_name, 3571 cast<ElaboratedType>(parent_qual_type)->getNamedType().getAsOpaquePtr(), 3572 idx, 3573 transparent_pointers, 3574 omit_empty_base_classes, 3575 ignore_array_bounds, 3576 child_name, 3577 child_byte_size, 3578 child_byte_offset, 3579 child_bitfield_bit_size, 3580 child_bitfield_bit_offset, 3581 child_is_base_class, 3582 child_is_deref_of_parent); 3583 3584 default: 3585 break; 3586 } 3587 } 3588 return NULL; 3589} 3590 3591static inline bool 3592BaseSpecifierIsEmpty (const CXXBaseSpecifier *b) 3593{ 3594 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false; 3595} 3596 3597static uint32_t 3598GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes) 3599{ 3600 uint32_t num_bases = 0; 3601 if (cxx_record_decl) 3602 { 3603 if (omit_empty_base_classes) 3604 { 3605 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 3606 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 3607 base_class != base_class_end; 3608 ++base_class) 3609 { 3610 // Skip empty base classes 3611 if (omit_empty_base_classes) 3612 { 3613 if (BaseSpecifierIsEmpty (base_class)) 3614 continue; 3615 } 3616 ++num_bases; 3617 } 3618 } 3619 else 3620 num_bases = cxx_record_decl->getNumBases(); 3621 } 3622 return num_bases; 3623} 3624 3625 3626static uint32_t 3627GetIndexForRecordBase 3628( 3629 const RecordDecl *record_decl, 3630 const CXXBaseSpecifier *base_spec, 3631 bool omit_empty_base_classes 3632) 3633{ 3634 uint32_t child_idx = 0; 3635 3636 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 3637 3638// const char *super_name = record_decl->getNameAsCString(); 3639// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString(); 3640// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name); 3641// 3642 if (cxx_record_decl) 3643 { 3644 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 3645 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 3646 base_class != base_class_end; 3647 ++base_class) 3648 { 3649 if (omit_empty_base_classes) 3650 { 3651 if (BaseSpecifierIsEmpty (base_class)) 3652 continue; 3653 } 3654 3655// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name, 3656// child_idx, 3657// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); 3658// 3659// 3660 if (base_class == base_spec) 3661 return child_idx; 3662 ++child_idx; 3663 } 3664 } 3665 3666 return UINT32_MAX; 3667} 3668 3669 3670static uint32_t 3671GetIndexForRecordChild 3672( 3673 const RecordDecl *record_decl, 3674 NamedDecl *canonical_decl, 3675 bool omit_empty_base_classes 3676) 3677{ 3678 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes); 3679 3680// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 3681// 3682//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString()); 3683// if (cxx_record_decl) 3684// { 3685// CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 3686// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 3687// base_class != base_class_end; 3688// ++base_class) 3689// { 3690// if (omit_empty_base_classes) 3691// { 3692// if (BaseSpecifierIsEmpty (base_class)) 3693// continue; 3694// } 3695// 3696//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", 3697//// record_decl->getNameAsCString(), 3698//// canonical_decl->getNameAsCString(), 3699//// child_idx, 3700//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); 3701// 3702// 3703// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 3704// if (curr_base_class_decl == canonical_decl) 3705// { 3706// return child_idx; 3707// } 3708// ++child_idx; 3709// } 3710// } 3711// 3712// const uint32_t num_bases = child_idx; 3713 RecordDecl::field_iterator field, field_end; 3714 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 3715 field != field_end; 3716 ++field, ++child_idx) 3717 { 3718// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n", 3719// record_decl->getNameAsCString(), 3720// canonical_decl->getNameAsCString(), 3721// child_idx - num_bases, 3722// field->getNameAsCString()); 3723 3724 if (field->getCanonicalDecl() == canonical_decl) 3725 return child_idx; 3726 } 3727 3728 return UINT32_MAX; 3729} 3730 3731// Look for a child member (doesn't include base classes, but it does include 3732// their members) in the type hierarchy. Returns an index path into "clang_type" 3733// on how to reach the appropriate member. 3734// 3735// class A 3736// { 3737// public: 3738// int m_a; 3739// int m_b; 3740// }; 3741// 3742// class B 3743// { 3744// }; 3745// 3746// class C : 3747// public B, 3748// public A 3749// { 3750// }; 3751// 3752// If we have a clang type that describes "class C", and we wanted to looked 3753// "m_b" in it: 3754// 3755// With omit_empty_base_classes == false we would get an integer array back with: 3756// { 1, 1 } 3757// The first index 1 is the child index for "class A" within class C 3758// The second index 1 is the child index for "m_b" within class A 3759// 3760// With omit_empty_base_classes == true we would get an integer array back with: 3761// { 0, 1 } 3762// The first index 0 is the child index for "class A" within class C (since class B doesn't have any members it doesn't count) 3763// The second index 1 is the child index for "m_b" within class A 3764 3765size_t 3766ClangASTContext::GetIndexOfChildMemberWithName 3767( 3768 ASTContext *ast, 3769 clang_type_t clang_type, 3770 const char *name, 3771 bool omit_empty_base_classes, 3772 std::vector<uint32_t>& child_indexes 3773) 3774{ 3775 if (clang_type && name && name[0]) 3776 { 3777 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 3778 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3779 switch (type_class) 3780 { 3781 case clang::Type::Record: 3782 if (GetCompleteQualType (ast, qual_type)) 3783 { 3784 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 3785 const RecordDecl *record_decl = record_type->getDecl(); 3786 3787 assert(record_decl); 3788 uint32_t child_idx = 0; 3789 3790 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 3791 3792 // Try and find a field that matches NAME 3793 RecordDecl::field_iterator field, field_end; 3794 StringRef name_sref(name); 3795 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 3796 field != field_end; 3797 ++field, ++child_idx) 3798 { 3799 if (field->getName().equals (name_sref)) 3800 { 3801 // We have to add on the number of base classes to this index! 3802 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes)); 3803 return child_indexes.size(); 3804 } 3805 } 3806 3807 if (cxx_record_decl) 3808 { 3809 const RecordDecl *parent_record_decl = cxx_record_decl; 3810 3811 //printf ("parent = %s\n", parent_record_decl->getNameAsCString()); 3812 3813 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl(); 3814 // Didn't find things easily, lets let clang do its thang... 3815 IdentifierInfo & ident_ref = ast->Idents.get(name_sref); 3816 DeclarationName decl_name(&ident_ref); 3817 3818 CXXBasePaths paths; 3819 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember, 3820 decl_name.getAsOpaquePtr(), 3821 paths)) 3822 { 3823 CXXBasePaths::const_paths_iterator path, path_end = paths.end(); 3824 for (path = paths.begin(); path != path_end; ++path) 3825 { 3826 const size_t num_path_elements = path->size(); 3827 for (size_t e=0; e<num_path_elements; ++e) 3828 { 3829 CXXBasePathElement elem = (*path)[e]; 3830 3831 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes); 3832 if (child_idx == UINT32_MAX) 3833 { 3834 child_indexes.clear(); 3835 return 0; 3836 } 3837 else 3838 { 3839 child_indexes.push_back (child_idx); 3840 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl()); 3841 } 3842 } 3843 DeclContext::lookup_iterator named_decl_pos; 3844 for (named_decl_pos = path->Decls.first; 3845 named_decl_pos != path->Decls.second && parent_record_decl; 3846 ++named_decl_pos) 3847 { 3848 //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString()); 3849 3850 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes); 3851 if (child_idx == UINT32_MAX) 3852 { 3853 child_indexes.clear(); 3854 return 0; 3855 } 3856 else 3857 { 3858 child_indexes.push_back (child_idx); 3859 } 3860 } 3861 } 3862 return child_indexes.size(); 3863 } 3864 } 3865 3866 } 3867 break; 3868 3869 case clang::Type::ObjCObject: 3870 case clang::Type::ObjCInterface: 3871 if (GetCompleteQualType (ast, qual_type)) 3872 { 3873 StringRef name_sref(name); 3874 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 3875 assert (objc_class_type); 3876 if (objc_class_type) 3877 { 3878 uint32_t child_idx = 0; 3879 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 3880 3881 if (class_interface_decl) 3882 { 3883 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 3884 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 3885 3886 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx) 3887 { 3888 const ObjCIvarDecl* ivar_decl = *ivar_pos; 3889 3890 if (ivar_decl->getName().equals (name_sref)) 3891 { 3892 if ((!omit_empty_base_classes && superclass_interface_decl) || 3893 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true))) 3894 ++child_idx; 3895 3896 child_indexes.push_back (child_idx); 3897 return child_indexes.size(); 3898 } 3899 } 3900 3901 if (superclass_interface_decl) 3902 { 3903 // The super class index is always zero for ObjC classes, 3904 // so we push it onto the child indexes in case we find 3905 // an ivar in our superclass... 3906 child_indexes.push_back (0); 3907 3908 if (GetIndexOfChildMemberWithName (ast, 3909 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), 3910 name, 3911 omit_empty_base_classes, 3912 child_indexes)) 3913 { 3914 // We did find an ivar in a superclass so just 3915 // return the results! 3916 return child_indexes.size(); 3917 } 3918 3919 // We didn't find an ivar matching "name" in our 3920 // superclass, pop the superclass zero index that 3921 // we pushed on above. 3922 child_indexes.pop_back(); 3923 } 3924 } 3925 } 3926 } 3927 break; 3928 3929 case clang::Type::ObjCObjectPointer: 3930 { 3931 return GetIndexOfChildMemberWithName (ast, 3932 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(), 3933 name, 3934 omit_empty_base_classes, 3935 child_indexes); 3936 } 3937 break; 3938 3939 3940 case clang::Type::ConstantArray: 3941 { 3942// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 3943// const uint64_t element_count = array->getSize().getLimitedValue(); 3944// 3945// if (idx < element_count) 3946// { 3947// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType()); 3948// 3949// char element_name[32]; 3950// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 3951// 3952// child_name.assign(element_name); 3953// assert(field_type_info.first % 8 == 0); 3954// child_byte_size = field_type_info.first / 8; 3955// child_byte_offset = idx * child_byte_size; 3956// return array->getElementType().getAsOpaquePtr(); 3957// } 3958 } 3959 break; 3960 3961// case clang::Type::MemberPointerType: 3962// { 3963// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); 3964// QualType pointee_type = mem_ptr_type->getPointeeType(); 3965// 3966// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3967// { 3968// return GetIndexOfChildWithName (ast, 3969// mem_ptr_type->getPointeeType().getAsOpaquePtr(), 3970// name); 3971// } 3972// } 3973// break; 3974// 3975 case clang::Type::LValueReference: 3976 case clang::Type::RValueReference: 3977 { 3978 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 3979 QualType pointee_type = reference_type->getPointeeType(); 3980 3981 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3982 { 3983 return GetIndexOfChildMemberWithName (ast, 3984 reference_type->getPointeeType().getAsOpaquePtr(), 3985 name, 3986 omit_empty_base_classes, 3987 child_indexes); 3988 } 3989 } 3990 break; 3991 3992 case clang::Type::Pointer: 3993 { 3994 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 3995 QualType pointee_type = pointer_type->getPointeeType(); 3996 3997 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3998 { 3999 return GetIndexOfChildMemberWithName (ast, 4000 pointer_type->getPointeeType().getAsOpaquePtr(), 4001 name, 4002 omit_empty_base_classes, 4003 child_indexes); 4004 } 4005 else 4006 { 4007// if (parent_name) 4008// { 4009// child_name.assign(1, '*'); 4010// child_name += parent_name; 4011// } 4012// 4013// // We have a pointer to an simple type 4014// if (idx == 0) 4015// { 4016// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 4017// assert(clang_type_info.first % 8 == 0); 4018// child_byte_size = clang_type_info.first / 8; 4019// child_byte_offset = 0; 4020// return pointee_type.getAsOpaquePtr(); 4021// } 4022 } 4023 } 4024 break; 4025 4026 case clang::Type::Typedef: 4027 return GetIndexOfChildMemberWithName (ast, 4028 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 4029 name, 4030 omit_empty_base_classes, 4031 child_indexes); 4032 4033 default: 4034 break; 4035 } 4036 } 4037 return 0; 4038} 4039 4040 4041// Get the index of the child of "clang_type" whose name matches. This function 4042// doesn't descend into the children, but only looks one level deep and name 4043// matches can include base class names. 4044 4045uint32_t 4046ClangASTContext::GetIndexOfChildWithName 4047( 4048 ASTContext *ast, 4049 clang_type_t clang_type, 4050 const char *name, 4051 bool omit_empty_base_classes 4052) 4053{ 4054 if (clang_type && name && name[0]) 4055 { 4056 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 4057 4058 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4059 4060 switch (type_class) 4061 { 4062 case clang::Type::Record: 4063 if (GetCompleteQualType (ast, qual_type)) 4064 { 4065 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 4066 const RecordDecl *record_decl = record_type->getDecl(); 4067 4068 assert(record_decl); 4069 uint32_t child_idx = 0; 4070 4071 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 4072 4073 if (cxx_record_decl) 4074 { 4075 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 4076 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 4077 base_class != base_class_end; 4078 ++base_class) 4079 { 4080 // Skip empty base classes 4081 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 4082 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false) 4083 continue; 4084 4085 std::string base_class_type_name (ClangASTType::GetTypeNameForQualType(base_class->getType())); 4086 if (base_class_type_name.compare (name) == 0) 4087 return child_idx; 4088 ++child_idx; 4089 } 4090 } 4091 4092 // Try and find a field that matches NAME 4093 RecordDecl::field_iterator field, field_end; 4094 StringRef name_sref(name); 4095 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 4096 field != field_end; 4097 ++field, ++child_idx) 4098 { 4099 if (field->getName().equals (name_sref)) 4100 return child_idx; 4101 } 4102 4103 } 4104 break; 4105 4106 case clang::Type::ObjCObject: 4107 case clang::Type::ObjCInterface: 4108 if (GetCompleteQualType (ast, qual_type)) 4109 { 4110 StringRef name_sref(name); 4111 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 4112 assert (objc_class_type); 4113 if (objc_class_type) 4114 { 4115 uint32_t child_idx = 0; 4116 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 4117 4118 if (class_interface_decl) 4119 { 4120 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 4121 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 4122 4123 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos) 4124 { 4125 const ObjCIvarDecl* ivar_decl = *ivar_pos; 4126 4127 if (ivar_decl->getName().equals (name_sref)) 4128 { 4129 if ((!omit_empty_base_classes && superclass_interface_decl) || 4130 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true))) 4131 ++child_idx; 4132 4133 return child_idx; 4134 } 4135 } 4136 4137 if (superclass_interface_decl) 4138 { 4139 if (superclass_interface_decl->getName().equals (name_sref)) 4140 return 0; 4141 } 4142 } 4143 } 4144 } 4145 break; 4146 4147 case clang::Type::ObjCObjectPointer: 4148 { 4149 return GetIndexOfChildWithName (ast, 4150 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(), 4151 name, 4152 omit_empty_base_classes); 4153 } 4154 break; 4155 4156 case clang::Type::ConstantArray: 4157 { 4158// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 4159// const uint64_t element_count = array->getSize().getLimitedValue(); 4160// 4161// if (idx < element_count) 4162// { 4163// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType()); 4164// 4165// char element_name[32]; 4166// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 4167// 4168// child_name.assign(element_name); 4169// assert(field_type_info.first % 8 == 0); 4170// child_byte_size = field_type_info.first / 8; 4171// child_byte_offset = idx * child_byte_size; 4172// return array->getElementType().getAsOpaquePtr(); 4173// } 4174 } 4175 break; 4176 4177// case clang::Type::MemberPointerType: 4178// { 4179// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); 4180// QualType pointee_type = mem_ptr_type->getPointeeType(); 4181// 4182// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 4183// { 4184// return GetIndexOfChildWithName (ast, 4185// mem_ptr_type->getPointeeType().getAsOpaquePtr(), 4186// name); 4187// } 4188// } 4189// break; 4190// 4191 case clang::Type::LValueReference: 4192 case clang::Type::RValueReference: 4193 { 4194 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 4195 QualType pointee_type = reference_type->getPointeeType(); 4196 4197 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 4198 { 4199 return GetIndexOfChildWithName (ast, 4200 reference_type->getPointeeType().getAsOpaquePtr(), 4201 name, 4202 omit_empty_base_classes); 4203 } 4204 } 4205 break; 4206 4207 case clang::Type::Pointer: 4208 { 4209 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 4210 QualType pointee_type = pointer_type->getPointeeType(); 4211 4212 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 4213 { 4214 return GetIndexOfChildWithName (ast, 4215 pointer_type->getPointeeType().getAsOpaquePtr(), 4216 name, 4217 omit_empty_base_classes); 4218 } 4219 else 4220 { 4221// if (parent_name) 4222// { 4223// child_name.assign(1, '*'); 4224// child_name += parent_name; 4225// } 4226// 4227// // We have a pointer to an simple type 4228// if (idx == 0) 4229// { 4230// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 4231// assert(clang_type_info.first % 8 == 0); 4232// child_byte_size = clang_type_info.first / 8; 4233// child_byte_offset = 0; 4234// return pointee_type.getAsOpaquePtr(); 4235// } 4236 } 4237 } 4238 break; 4239 4240 case clang::Type::Typedef: 4241 return GetIndexOfChildWithName (ast, 4242 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 4243 name, 4244 omit_empty_base_classes); 4245 4246 default: 4247 break; 4248 } 4249 } 4250 return UINT32_MAX; 4251} 4252 4253#pragma mark TagType 4254 4255bool 4256ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind) 4257{ 4258 if (tag_clang_type) 4259 { 4260 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type)); 4261 const clang::Type *clang_type = tag_qual_type.getTypePtr(); 4262 if (clang_type) 4263 { 4264 const TagType *tag_type = dyn_cast<TagType>(clang_type); 4265 if (tag_type) 4266 { 4267 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl()); 4268 if (tag_decl) 4269 { 4270 tag_decl->setTagKind ((TagDecl::TagKind)kind); 4271 return true; 4272 } 4273 } 4274 } 4275 } 4276 return false; 4277} 4278 4279 4280#pragma mark DeclContext Functions 4281 4282DeclContext * 4283ClangASTContext::GetDeclContextForType (clang_type_t clang_type) 4284{ 4285 if (clang_type == NULL) 4286 return NULL; 4287 4288 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 4289 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4290 switch (type_class) 4291 { 4292 case clang::Type::UnaryTransform: break; 4293 case clang::Type::FunctionNoProto: break; 4294 case clang::Type::FunctionProto: break; 4295 case clang::Type::IncompleteArray: break; 4296 case clang::Type::VariableArray: break; 4297 case clang::Type::ConstantArray: break; 4298 case clang::Type::DependentSizedArray: break; 4299 case clang::Type::ExtVector: break; 4300 case clang::Type::DependentSizedExtVector: break; 4301 case clang::Type::Vector: break; 4302 case clang::Type::Builtin: break; 4303 case clang::Type::BlockPointer: break; 4304 case clang::Type::Pointer: break; 4305 case clang::Type::LValueReference: break; 4306 case clang::Type::RValueReference: break; 4307 case clang::Type::MemberPointer: break; 4308 case clang::Type::Complex: break; 4309 case clang::Type::ObjCObject: break; 4310 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface(); 4311 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr()); 4312 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl(); 4313 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl(); 4314 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 4315 case clang::Type::Elaborated: return ClangASTContext::GetDeclContextForType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 4316 case clang::Type::TypeOfExpr: break; 4317 case clang::Type::TypeOf: break; 4318 case clang::Type::Decltype: break; 4319 //case clang::Type::QualifiedName: break; 4320 case clang::Type::TemplateSpecialization: break; 4321 case clang::Type::DependentTemplateSpecialization: break; 4322 case clang::Type::TemplateTypeParm: break; 4323 case clang::Type::SubstTemplateTypeParm: break; 4324 case clang::Type::SubstTemplateTypeParmPack:break; 4325 case clang::Type::PackExpansion: break; 4326 case clang::Type::UnresolvedUsing: break; 4327 case clang::Type::Paren: break; 4328 case clang::Type::Attributed: break; 4329 case clang::Type::Auto: break; 4330 case clang::Type::InjectedClassName: break; 4331 case clang::Type::DependentName: break; 4332 case clang::Type::Atomic: break; 4333 } 4334 // No DeclContext in this type... 4335 return NULL; 4336} 4337 4338#pragma mark Namespace Declarations 4339 4340NamespaceDecl * 4341ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx) 4342{ 4343 NamespaceDecl *namespace_decl = NULL; 4344 ASTContext *ast = getASTContext(); 4345 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl (); 4346 if (decl_ctx == NULL) 4347 decl_ctx = translation_unit_decl; 4348 4349 if (name) 4350 { 4351 IdentifierInfo &identifier_info = ast->Idents.get(name); 4352 DeclarationName decl_name (&identifier_info); 4353 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); 4354 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos) 4355 { 4356 namespace_decl = dyn_cast<clang::NamespaceDecl>(*pos); 4357 if (namespace_decl) 4358 return namespace_decl; 4359 } 4360 4361 namespace_decl = NamespaceDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), &identifier_info); 4362 4363 decl_ctx->addDecl (namespace_decl); 4364 } 4365 else 4366 { 4367 if (decl_ctx == translation_unit_decl) 4368 { 4369 namespace_decl = translation_unit_decl->getAnonymousNamespace(); 4370 if (namespace_decl) 4371 return namespace_decl; 4372 4373 namespace_decl = NamespaceDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), NULL); 4374 translation_unit_decl->setAnonymousNamespace (namespace_decl); 4375 translation_unit_decl->addDecl (namespace_decl); 4376 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace()); 4377 } 4378 else 4379 { 4380 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx); 4381 if (parent_namespace_decl) 4382 { 4383 namespace_decl = parent_namespace_decl->getAnonymousNamespace(); 4384 if (namespace_decl) 4385 return namespace_decl; 4386 namespace_decl = NamespaceDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), NULL); 4387 parent_namespace_decl->setAnonymousNamespace (namespace_decl); 4388 parent_namespace_decl->addDecl (namespace_decl); 4389 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace()); 4390 } 4391 else 4392 { 4393 // BAD!!! 4394 } 4395 } 4396 4397 4398 if (namespace_decl) 4399 { 4400 // If we make it here, we are creating the anonymous namespace decl 4401 // for the first time, so we need to do the using directive magic 4402 // like SEMA does 4403 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast, 4404 decl_ctx, 4405 SourceLocation(), 4406 SourceLocation(), 4407 NestedNameSpecifierLoc(), 4408 SourceLocation(), 4409 namespace_decl, 4410 decl_ctx); 4411 using_directive_decl->setImplicit(); 4412 decl_ctx->addDecl(using_directive_decl); 4413 } 4414 } 4415#ifdef LLDB_CONFIGURATION_DEBUG 4416 VerifyDecl(namespace_decl); 4417#endif 4418 return namespace_decl; 4419} 4420 4421 4422#pragma mark Function Types 4423 4424FunctionDecl * 4425ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx, const char *name, clang_type_t function_clang_type, int storage, bool is_inline) 4426{ 4427 FunctionDecl *func_decl = NULL; 4428 ASTContext *ast = getASTContext(); 4429 if (decl_ctx == NULL) 4430 decl_ctx = ast->getTranslationUnitDecl(); 4431 4432 if (name && name[0]) 4433 { 4434 func_decl = FunctionDecl::Create (*ast, 4435 decl_ctx, 4436 SourceLocation(), 4437 SourceLocation(), 4438 DeclarationName (&ast->Idents.get(name)), 4439 QualType::getFromOpaquePtr(function_clang_type), 4440 NULL, 4441 (FunctionDecl::StorageClass)storage, 4442 (FunctionDecl::StorageClass)storage, 4443 is_inline); 4444 } 4445 else 4446 { 4447 func_decl = FunctionDecl::Create (*ast, 4448 decl_ctx, 4449 SourceLocation(), 4450 SourceLocation(), 4451 DeclarationName (), 4452 QualType::getFromOpaquePtr(function_clang_type), 4453 NULL, 4454 (FunctionDecl::StorageClass)storage, 4455 (FunctionDecl::StorageClass)storage, 4456 is_inline); 4457 } 4458 if (func_decl) 4459 decl_ctx->addDecl (func_decl); 4460 4461#ifdef LLDB_CONFIGURATION_DEBUG 4462 VerifyDecl(func_decl); 4463#endif 4464 4465 return func_decl; 4466} 4467 4468clang_type_t 4469ClangASTContext::CreateFunctionType (ASTContext *ast, 4470 clang_type_t result_type, 4471 clang_type_t *args, 4472 unsigned num_args, 4473 bool is_variadic, 4474 unsigned type_quals) 4475{ 4476 assert (ast != NULL); 4477 std::vector<QualType> qual_type_args; 4478 for (unsigned i=0; i<num_args; ++i) 4479 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i])); 4480 4481 // TODO: Detect calling convention in DWARF? 4482 FunctionProtoType::ExtProtoInfo proto_info; 4483 proto_info.Variadic = is_variadic; 4484 proto_info.ExceptionSpecType = EST_None; 4485 proto_info.TypeQuals = type_quals; 4486 proto_info.RefQualifier = RQ_None; 4487 proto_info.NumExceptions = 0; 4488 proto_info.Exceptions = NULL; 4489 4490 return ast->getFunctionType (QualType::getFromOpaquePtr(result_type), 4491 qual_type_args.empty() ? NULL : &qual_type_args.front(), 4492 qual_type_args.size(), 4493 proto_info).getAsOpaquePtr(); // NoReturn); 4494} 4495 4496ParmVarDecl * 4497ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage) 4498{ 4499 ASTContext *ast = getASTContext(); 4500 assert (ast != NULL); 4501 return ParmVarDecl::Create(*ast, 4502 ast->getTranslationUnitDecl(), 4503 SourceLocation(), 4504 SourceLocation(), 4505 name && name[0] ? &ast->Idents.get(name) : NULL, 4506 QualType::getFromOpaquePtr(param_type), 4507 NULL, 4508 (VarDecl::StorageClass)storage, 4509 (VarDecl::StorageClass)storage, 4510 0); 4511} 4512 4513void 4514ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params) 4515{ 4516 if (function_decl) 4517 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params)); 4518} 4519 4520 4521#pragma mark Array Types 4522 4523clang_type_t 4524ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count, uint32_t bit_stride) 4525{ 4526 if (element_type) 4527 { 4528 ASTContext *ast = getASTContext(); 4529 assert (ast != NULL); 4530 llvm::APInt ap_element_count (64, element_count); 4531 return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type), 4532 ap_element_count, 4533 ArrayType::Normal, 4534 0).getAsOpaquePtr(); // ElemQuals 4535 } 4536 return NULL; 4537} 4538 4539 4540#pragma mark TagDecl 4541 4542bool 4543ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type) 4544{ 4545 if (clang_type) 4546 { 4547 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4548 const clang::Type *t = qual_type.getTypePtr(); 4549 if (t) 4550 { 4551 const TagType *tag_type = dyn_cast<TagType>(t); 4552 if (tag_type) 4553 { 4554 TagDecl *tag_decl = tag_type->getDecl(); 4555 if (tag_decl) 4556 { 4557 tag_decl->startDefinition(); 4558 return true; 4559 } 4560 } 4561 } 4562 } 4563 return false; 4564} 4565 4566bool 4567ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type) 4568{ 4569 if (clang_type) 4570 { 4571 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4572 4573 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 4574 4575 if (cxx_record_decl) 4576 { 4577 cxx_record_decl->completeDefinition(); 4578 4579 return true; 4580 } 4581 4582 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type); 4583 4584 if (objc_class_type) 4585 { 4586 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 4587 4588 class_interface_decl->setForwardDecl(false); 4589 } 4590 4591 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr()); 4592 4593 if (enum_type) 4594 { 4595 EnumDecl *enum_decl = enum_type->getDecl(); 4596 4597 if (enum_decl) 4598 { 4599 /// TODO This really needs to be fixed. 4600 4601 unsigned NumPositiveBits = 1; 4602 unsigned NumNegativeBits = 0; 4603 4604 ASTContext *ast = getASTContext(); 4605 4606 QualType promotion_qual_type; 4607 // If the enum integer type is less than an integer in bit width, 4608 // then we must promote it to an integer size. 4609 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy)) 4610 { 4611 if (enum_decl->getIntegerType()->isSignedIntegerType()) 4612 promotion_qual_type = ast->IntTy; 4613 else 4614 promotion_qual_type = ast->UnsignedIntTy; 4615 } 4616 else 4617 promotion_qual_type = enum_decl->getIntegerType(); 4618 4619 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits); 4620 return true; 4621 } 4622 } 4623 } 4624 return false; 4625} 4626 4627 4628#pragma mark Enumeration Types 4629 4630clang_type_t 4631ClangASTContext::CreateEnumerationType 4632( 4633 const char *name, 4634 DeclContext *decl_ctx, 4635 const Declaration &decl, 4636 clang_type_t integer_qual_type 4637) 4638{ 4639 // TODO: Do something intelligent with the Declaration object passed in 4640 // like maybe filling in the SourceLocation with it... 4641 ASTContext *ast = getASTContext(); 4642 assert (ast != NULL); 4643 4644 // TODO: ask about these... 4645// const bool IsScoped = false; 4646// const bool IsFixed = false; 4647 4648 EnumDecl *enum_decl = EnumDecl::Create (*ast, 4649 decl_ctx, 4650 SourceLocation(), 4651 SourceLocation(), 4652 name && name[0] ? &ast->Idents.get(name) : NULL, 4653 NULL, 4654 false, // IsScoped 4655 false, // IsScopedUsingClassTag 4656 false); // IsFixed 4657 4658 4659 if (enum_decl) 4660 { 4661 // TODO: check if we should be setting the promotion type too? 4662 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type)); 4663 4664 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info 4665 4666 return ast->getTagDeclType(enum_decl).getAsOpaquePtr(); 4667 } 4668 return NULL; 4669} 4670 4671clang_type_t 4672ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type) 4673{ 4674 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type)); 4675 4676 const clang::Type *clang_type = enum_qual_type.getTypePtr(); 4677 if (clang_type) 4678 { 4679 const EnumType *enum_type = dyn_cast<EnumType>(clang_type); 4680 if (enum_type) 4681 { 4682 EnumDecl *enum_decl = enum_type->getDecl(); 4683 if (enum_decl) 4684 return enum_decl->getIntegerType().getAsOpaquePtr(); 4685 } 4686 } 4687 return NULL; 4688} 4689bool 4690ClangASTContext::AddEnumerationValueToEnumerationType 4691( 4692 clang_type_t enum_clang_type, 4693 clang_type_t enumerator_clang_type, 4694 const Declaration &decl, 4695 const char *name, 4696 int64_t enum_value, 4697 uint32_t enum_value_bit_size 4698) 4699{ 4700 if (enum_clang_type && enumerator_clang_type && name) 4701 { 4702 // TODO: Do something intelligent with the Declaration object passed in 4703 // like maybe filling in the SourceLocation with it... 4704 ASTContext *ast = getASTContext(); 4705 IdentifierTable *identifier_table = getIdentifierTable(); 4706 4707 assert (ast != NULL); 4708 assert (identifier_table != NULL); 4709 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type)); 4710 4711 const clang::Type *clang_type = enum_qual_type.getTypePtr(); 4712 if (clang_type) 4713 { 4714 const EnumType *enum_type = dyn_cast<EnumType>(clang_type); 4715 4716 if (enum_type) 4717 { 4718 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false); 4719 enum_llvm_apsint = enum_value; 4720 EnumConstantDecl *enumerator_decl = 4721 EnumConstantDecl::Create (*ast, 4722 enum_type->getDecl(), 4723 SourceLocation(), 4724 name ? &identifier_table->get(name) : NULL, // Identifier 4725 QualType::getFromOpaquePtr(enumerator_clang_type), 4726 NULL, 4727 enum_llvm_apsint); 4728 4729 if (enumerator_decl) 4730 { 4731 enum_type->getDecl()->addDecl(enumerator_decl); 4732 4733#ifdef LLDB_CONFIGURATION_DEBUG 4734 VerifyDecl(enumerator_decl); 4735#endif 4736 4737 return true; 4738 } 4739 } 4740 } 4741 } 4742 return false; 4743} 4744 4745#pragma mark Pointers & References 4746 4747clang_type_t 4748ClangASTContext::CreatePointerType (clang_type_t clang_type) 4749{ 4750 return CreatePointerType (getASTContext(), clang_type); 4751} 4752 4753clang_type_t 4754ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type) 4755{ 4756 if (ast && clang_type) 4757 { 4758 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4759 4760 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4761 switch (type_class) 4762 { 4763 case clang::Type::ObjCObject: 4764 case clang::Type::ObjCInterface: 4765 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr(); 4766 4767 default: 4768 return ast->getPointerType(qual_type).getAsOpaquePtr(); 4769 } 4770 } 4771 return NULL; 4772} 4773 4774clang_type_t 4775ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast, 4776 clang_type_t clang_type) 4777{ 4778 if (clang_type) 4779 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 4780 return NULL; 4781} 4782 4783clang_type_t 4784ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast, 4785 clang_type_t clang_type) 4786{ 4787 if (clang_type) 4788 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 4789 return NULL; 4790} 4791 4792clang_type_t 4793ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type) 4794{ 4795 if (clang_pointee_type && clang_pointee_type) 4796 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type), 4797 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr(); 4798 return NULL; 4799} 4800 4801uint32_t 4802ClangASTContext::GetPointerBitSize () 4803{ 4804 ASTContext *ast = getASTContext(); 4805 return ast->getTypeSize(ast->VoidPtrTy); 4806} 4807 4808bool 4809ClangASTContext::IsPossibleDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type) 4810{ 4811 QualType pointee_qual_type; 4812 if (clang_type) 4813 { 4814 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4815 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4816 bool success = false; 4817 switch (type_class) 4818 { 4819 case clang::Type::Builtin: 4820 if (cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId) 4821 { 4822 if (dynamic_pointee_type) 4823 *dynamic_pointee_type = clang_type; 4824 return true; 4825 } 4826 break; 4827 4828 case clang::Type::ObjCObjectPointer: 4829 if (dynamic_pointee_type) 4830 *dynamic_pointee_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 4831 return true; 4832 4833 case clang::Type::Pointer: 4834 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType(); 4835 success = true; 4836 break; 4837 4838 case clang::Type::LValueReference: 4839 case clang::Type::RValueReference: 4840 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType(); 4841 success = true; 4842 break; 4843 4844 case clang::Type::Typedef: 4845 return ClangASTContext::IsPossibleDynamicType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), dynamic_pointee_type); 4846 4847 case clang::Type::Elaborated: 4848 return ClangASTContext::IsPossibleDynamicType (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), dynamic_pointee_type); 4849 4850 default: 4851 break; 4852 } 4853 4854 if (success) 4855 { 4856 // Check to make sure what we are pointing too is a possible dynamic C++ type 4857 // We currently accept any "void *" (in case we have a class that has been 4858 // watered down to an opaque pointer) and virtual C++ classes. 4859 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass(); 4860 switch (pointee_type_class) 4861 { 4862 case clang::Type::Builtin: 4863 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind()) 4864 { 4865 case clang::BuiltinType::UnknownAny: 4866 case clang::BuiltinType::Void: 4867 if (dynamic_pointee_type) 4868 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr(); 4869 return true; 4870 4871 case clang::BuiltinType::NullPtr: 4872 case clang::BuiltinType::Bool: 4873 case clang::BuiltinType::Char_U: 4874 case clang::BuiltinType::UChar: 4875 case clang::BuiltinType::WChar_U: 4876 case clang::BuiltinType::Char16: 4877 case clang::BuiltinType::Char32: 4878 case clang::BuiltinType::UShort: 4879 case clang::BuiltinType::UInt: 4880 case clang::BuiltinType::ULong: 4881 case clang::BuiltinType::ULongLong: 4882 case clang::BuiltinType::UInt128: 4883 case clang::BuiltinType::Char_S: 4884 case clang::BuiltinType::SChar: 4885 case clang::BuiltinType::WChar_S: 4886 case clang::BuiltinType::Short: 4887 case clang::BuiltinType::Int: 4888 case clang::BuiltinType::Long: 4889 case clang::BuiltinType::LongLong: 4890 case clang::BuiltinType::Int128: 4891 case clang::BuiltinType::Float: 4892 case clang::BuiltinType::Double: 4893 case clang::BuiltinType::LongDouble: 4894 case clang::BuiltinType::Dependent: 4895 case clang::BuiltinType::Overload: 4896 case clang::BuiltinType::ObjCId: 4897 case clang::BuiltinType::ObjCClass: 4898 case clang::BuiltinType::ObjCSel: 4899 case clang::BuiltinType::BoundMember: 4900 case clang::BuiltinType::Half: 4901 case clang::BuiltinType::ARCUnbridgedCast: 4902 case clang::BuiltinType::PseudoObject: 4903 break; 4904 } 4905 break; 4906 4907 case clang::Type::Record: 4908 { 4909 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl(); 4910 if (cxx_record_decl) 4911 { 4912 if (GetCompleteQualType (ast, pointee_qual_type)) 4913 { 4914 success = cxx_record_decl->isDynamicClass(); 4915 } 4916 else 4917 { 4918 // We failed to get the complete type, so we have to 4919 // treat this as a void * which we might possibly be 4920 // able to complete 4921 success = true; 4922 } 4923 if (success) 4924 { 4925 if (dynamic_pointee_type) 4926 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr(); 4927 return true; 4928 } 4929 } 4930 } 4931 break; 4932 4933 case clang::Type::ObjCObject: 4934 case clang::Type::ObjCInterface: 4935 { 4936 const clang::ObjCObjectType *objc_class_type = pointee_qual_type->getAsObjCQualifiedInterfaceType(); 4937 if (objc_class_type) 4938 { 4939 GetCompleteQualType (ast, pointee_qual_type); 4940 if (dynamic_pointee_type) 4941 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr(); 4942 return true; 4943 } 4944 } 4945 break; 4946 4947 default: 4948 break; 4949 } 4950 } 4951 } 4952 if (dynamic_pointee_type) 4953 *dynamic_pointee_type = NULL; 4954 return false; 4955} 4956 4957 4958bool 4959ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type) 4960{ 4961 QualType pointee_qual_type; 4962 if (clang_type) 4963 { 4964 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4965 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4966 bool success = false; 4967 switch (type_class) 4968 { 4969 case clang::Type::Pointer: 4970 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType(); 4971 success = true; 4972 break; 4973 4974 case clang::Type::LValueReference: 4975 case clang::Type::RValueReference: 4976 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType(); 4977 success = true; 4978 break; 4979 4980 case clang::Type::Typedef: 4981 return ClangASTContext::IsPossibleCPlusPlusDynamicType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), dynamic_pointee_type); 4982 4983 case clang::Type::Elaborated: 4984 return ClangASTContext::IsPossibleCPlusPlusDynamicType (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 4985 4986 default: 4987 break; 4988 } 4989 4990 if (success) 4991 { 4992 // Check to make sure what we are pointing too is a possible dynamic C++ type 4993 // We currently accept any "void *" (in case we have a class that has been 4994 // watered down to an opaque pointer) and virtual C++ classes. 4995 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass(); 4996 switch (pointee_type_class) 4997 { 4998 case clang::Type::Builtin: 4999 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind()) 5000 { 5001 case clang::BuiltinType::UnknownAny: 5002 case clang::BuiltinType::Void: 5003 if (dynamic_pointee_type) 5004 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr(); 5005 return true; 5006 5007 case clang::BuiltinType::NullPtr: 5008 case clang::BuiltinType::Bool: 5009 case clang::BuiltinType::Char_U: 5010 case clang::BuiltinType::UChar: 5011 case clang::BuiltinType::WChar_U: 5012 case clang::BuiltinType::Char16: 5013 case clang::BuiltinType::Char32: 5014 case clang::BuiltinType::UShort: 5015 case clang::BuiltinType::UInt: 5016 case clang::BuiltinType::ULong: 5017 case clang::BuiltinType::ULongLong: 5018 case clang::BuiltinType::UInt128: 5019 case clang::BuiltinType::Char_S: 5020 case clang::BuiltinType::SChar: 5021 case clang::BuiltinType::WChar_S: 5022 case clang::BuiltinType::Short: 5023 case clang::BuiltinType::Int: 5024 case clang::BuiltinType::Long: 5025 case clang::BuiltinType::LongLong: 5026 case clang::BuiltinType::Int128: 5027 case clang::BuiltinType::Float: 5028 case clang::BuiltinType::Double: 5029 case clang::BuiltinType::LongDouble: 5030 case clang::BuiltinType::Dependent: 5031 case clang::BuiltinType::Overload: 5032 case clang::BuiltinType::ObjCId: 5033 case clang::BuiltinType::ObjCClass: 5034 case clang::BuiltinType::ObjCSel: 5035 case clang::BuiltinType::BoundMember: 5036 case clang::BuiltinType::Half: 5037 case clang::BuiltinType::ARCUnbridgedCast: 5038 case clang::BuiltinType::PseudoObject: 5039 break; 5040 } 5041 break; 5042 case clang::Type::Record: 5043 { 5044 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl(); 5045 if (cxx_record_decl) 5046 { 5047 if (GetCompleteQualType (ast, pointee_qual_type)) 5048 { 5049 success = cxx_record_decl->isDynamicClass(); 5050 } 5051 else 5052 { 5053 // We failed to get the complete type, so we have to 5054 // treat this as a void * which we might possibly be 5055 // able to complete 5056 success = true; 5057 } 5058 if (success) 5059 { 5060 if (dynamic_pointee_type) 5061 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr(); 5062 return true; 5063 } 5064 } 5065 } 5066 break; 5067 5068 default: 5069 break; 5070 } 5071 } 5072 } 5073 if (dynamic_pointee_type) 5074 *dynamic_pointee_type = NULL; 5075 return false; 5076} 5077 5078bool 5079ClangASTContext::IsReferenceType (clang_type_t clang_type, clang_type_t *target_type) 5080{ 5081 if (clang_type == NULL) 5082 return false; 5083 5084 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5085 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5086 5087 switch (type_class) 5088 { 5089 case clang::Type::LValueReference: 5090 if (target_type) 5091 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 5092 return true; 5093 case clang::Type::RValueReference: 5094 if (target_type) 5095 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 5096 return true; 5097 case clang::Type::Typedef: 5098 return ClangASTContext::IsReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 5099 case clang::Type::Elaborated: 5100 return ClangASTContext::IsReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 5101 default: 5102 break; 5103 } 5104 5105 return false; 5106} 5107 5108bool 5109ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type) 5110{ 5111 if (clang_type == NULL) 5112 return false; 5113 5114 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5115 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5116 switch (type_class) 5117 { 5118 case clang::Type::Builtin: 5119 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 5120 { 5121 default: 5122 break; 5123 case clang::BuiltinType::ObjCId: 5124 case clang::BuiltinType::ObjCClass: 5125 return true; 5126 } 5127 return false; 5128 case clang::Type::ObjCObjectPointer: 5129 if (target_type) 5130 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5131 return true; 5132 case clang::Type::BlockPointer: 5133 if (target_type) 5134 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5135 return true; 5136 case clang::Type::Pointer: 5137 if (target_type) 5138 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5139 return true; 5140 case clang::Type::MemberPointer: 5141 if (target_type) 5142 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5143 return true; 5144 case clang::Type::LValueReference: 5145 if (target_type) 5146 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 5147 return true; 5148 case clang::Type::RValueReference: 5149 if (target_type) 5150 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 5151 return true; 5152 case clang::Type::Typedef: 5153 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 5154 case clang::Type::Elaborated: 5155 return ClangASTContext::IsPointerOrReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 5156 default: 5157 break; 5158 } 5159 return false; 5160} 5161 5162bool 5163ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed) 5164{ 5165 if (!clang_type) 5166 return false; 5167 5168 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5169 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()); 5170 5171 if (builtin_type) 5172 { 5173 if (builtin_type->isInteger()) 5174 is_signed = builtin_type->isSignedInteger(); 5175 5176 return true; 5177 } 5178 5179 return false; 5180} 5181 5182bool 5183ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t *target_type) 5184{ 5185 if (target_type) 5186 *target_type = NULL; 5187 5188 if (clang_type) 5189 { 5190 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5191 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5192 switch (type_class) 5193 { 5194 case clang::Type::Builtin: 5195 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 5196 { 5197 default: 5198 break; 5199 case clang::BuiltinType::ObjCId: 5200 case clang::BuiltinType::ObjCClass: 5201 return true; 5202 } 5203 return false; 5204 case clang::Type::ObjCObjectPointer: 5205 if (target_type) 5206 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5207 return true; 5208 case clang::Type::BlockPointer: 5209 if (target_type) 5210 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5211 return true; 5212 case clang::Type::Pointer: 5213 if (target_type) 5214 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5215 return true; 5216 case clang::Type::MemberPointer: 5217 if (target_type) 5218 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5219 return true; 5220 case clang::Type::Typedef: 5221 return ClangASTContext::IsPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type); 5222 case clang::Type::Elaborated: 5223 return ClangASTContext::IsPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), target_type); 5224 default: 5225 break; 5226 } 5227 } 5228 return false; 5229} 5230 5231bool 5232ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex) 5233{ 5234 if (clang_type) 5235 { 5236 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5237 5238 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal())) 5239 { 5240 clang::BuiltinType::Kind kind = BT->getKind(); 5241 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble) 5242 { 5243 count = 1; 5244 is_complex = false; 5245 return true; 5246 } 5247 } 5248 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal())) 5249 { 5250 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex)) 5251 { 5252 count = 2; 5253 is_complex = true; 5254 return true; 5255 } 5256 } 5257 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal())) 5258 { 5259 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex)) 5260 { 5261 count = VT->getNumElements(); 5262 is_complex = false; 5263 return true; 5264 } 5265 } 5266 } 5267 return false; 5268} 5269 5270bool 5271ClangASTContext::IsScalarType (lldb::clang_type_t clang_type) 5272{ 5273 bool is_signed; 5274 if (ClangASTContext::IsIntegerType(clang_type, is_signed)) 5275 return true; 5276 5277 uint32_t count; 5278 bool is_complex; 5279 return ClangASTContext::IsFloatingPointType(clang_type, count, is_complex) && !is_complex; 5280} 5281 5282bool 5283ClangASTContext::IsPointerToScalarType (lldb::clang_type_t clang_type) 5284{ 5285 if (!IsPointerType(clang_type)) 5286 return false; 5287 5288 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5289 lldb::clang_type_t pointee_type = qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr(); 5290 return IsScalarType(pointee_type); 5291} 5292 5293bool 5294ClangASTContext::IsArrayOfScalarType (lldb::clang_type_t clang_type) 5295{ 5296 if (!IsArrayType(clang_type)) 5297 return false; 5298 5299 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5300 lldb::clang_type_t item_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr(); 5301 return IsScalarType(item_type); 5302} 5303 5304 5305bool 5306ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name) 5307{ 5308 if (clang_type) 5309 { 5310 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5311 5312 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 5313 if (cxx_record_decl) 5314 { 5315 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart()); 5316 return true; 5317 } 5318 } 5319 class_name.clear(); 5320 return false; 5321} 5322 5323 5324bool 5325ClangASTContext::IsCXXClassType (clang_type_t clang_type) 5326{ 5327 if (clang_type) 5328 { 5329 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5330 if (qual_type->getAsCXXRecordDecl() != NULL) 5331 return true; 5332 } 5333 return false; 5334} 5335 5336bool 5337ClangASTContext::IsBeingDefined (lldb::clang_type_t clang_type) 5338{ 5339 if (clang_type) 5340 { 5341 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5342 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type); 5343 if (tag_type) 5344 return tag_type->isBeingDefined(); 5345 } 5346 return false; 5347} 5348 5349bool 5350ClangASTContext::IsObjCClassType (clang_type_t clang_type) 5351{ 5352 if (clang_type) 5353 { 5354 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5355 if (qual_type->isObjCObjectOrInterfaceType()) 5356 return true; 5357 } 5358 return false; 5359} 5360 5361 5362bool 5363ClangASTContext::IsCharType (clang_type_t clang_type) 5364{ 5365 if (clang_type) 5366 return QualType::getFromOpaquePtr(clang_type)->isCharType(); 5367 return false; 5368} 5369 5370bool 5371ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length) 5372{ 5373 clang_type_t pointee_or_element_clang_type = NULL; 5374 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type)); 5375 5376 if (pointee_or_element_clang_type == NULL) 5377 return false; 5378 5379 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer)) 5380 { 5381 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type)); 5382 5383 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType()) 5384 { 5385 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5386 if (type_flags.Test (eTypeIsArray)) 5387 { 5388 // We know the size of the array and it could be a C string 5389 // since it is an array of characters 5390 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue(); 5391 return true; 5392 } 5393 else 5394 { 5395 length = 0; 5396 return true; 5397 } 5398 5399 } 5400 } 5401 return false; 5402} 5403 5404bool 5405ClangASTContext::IsFunctionPointerType (clang_type_t clang_type) 5406{ 5407 if (clang_type) 5408 { 5409 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5410 5411 if (qual_type->isFunctionPointerType()) 5412 return true; 5413 5414 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5415 switch (type_class) 5416 { 5417 default: 5418 break; 5419 case clang::Type::Typedef: 5420 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 5421 case clang::Type::Elaborated: 5422 return ClangASTContext::IsFunctionPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 5423 5424 case clang::Type::LValueReference: 5425 case clang::Type::RValueReference: 5426 { 5427 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 5428 if (reference_type) 5429 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr()); 5430 } 5431 break; 5432 } 5433 } 5434 return false; 5435} 5436 5437size_t 5438ClangASTContext::GetArraySize (clang_type_t clang_type) 5439{ 5440 if (clang_type) 5441 { 5442 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 5443 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5444 switch (type_class) 5445 { 5446 case clang::Type::ConstantArray: 5447 { 5448 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr()); 5449 if (array) 5450 return array->getSize().getLimitedValue(); 5451 } 5452 break; 5453 5454 case clang::Type::Typedef: 5455 return ClangASTContext::GetArraySize(cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 5456 5457 case clang::Type::Elaborated: 5458 return ClangASTContext::GetArraySize(cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 5459 5460 default: 5461 break; 5462 } 5463 } 5464 return 0; 5465} 5466 5467bool 5468ClangASTContext::IsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size) 5469{ 5470 if (!clang_type) 5471 return false; 5472 5473 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5474 5475 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5476 switch (type_class) 5477 { 5478 default: 5479 break; 5480 5481 case clang::Type::ConstantArray: 5482 if (member_type) 5483 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 5484 if (size) 5485 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX); 5486 return true; 5487 5488 case clang::Type::IncompleteArray: 5489 if (member_type) 5490 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 5491 if (size) 5492 *size = 0; 5493 return true; 5494 5495 case clang::Type::VariableArray: 5496 if (member_type) 5497 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 5498 if (size) 5499 *size = 0; 5500 return true; 5501 5502 case clang::Type::DependentSizedArray: 5503 if (member_type) 5504 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 5505 if (size) 5506 *size = 0; 5507 return true; 5508 5509 case clang::Type::Typedef: 5510 return ClangASTContext::IsArrayType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 5511 member_type, 5512 size); 5513 5514 case clang::Type::Elaborated: 5515 return ClangASTContext::IsArrayType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), 5516 member_type, 5517 size); 5518 } 5519 return false; 5520} 5521 5522 5523#pragma mark Typedefs 5524 5525clang_type_t 5526ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx) 5527{ 5528 if (clang_type) 5529 { 5530 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5531 ASTContext *ast = getASTContext(); 5532 IdentifierTable *identifier_table = getIdentifierTable(); 5533 assert (ast != NULL); 5534 assert (identifier_table != NULL); 5535 if (decl_ctx == NULL) 5536 decl_ctx = ast->getTranslationUnitDecl(); 5537 TypedefDecl *decl = TypedefDecl::Create (*ast, 5538 decl_ctx, 5539 SourceLocation(), 5540 SourceLocation(), 5541 name ? &identifier_table->get(name) : NULL, // Identifier 5542 ast->CreateTypeSourceInfo(qual_type)); 5543 5544 //decl_ctx->addDecl (decl); 5545 5546 decl->setAccess(AS_public); // TODO respect proper access specifier 5547 5548 // Get a uniqued QualType for the typedef decl type 5549 return ast->getTypedefType (decl).getAsOpaquePtr(); 5550 } 5551 return NULL; 5552} 5553 5554// Disable this for now since I can't seem to get a nicely formatted float 5555// out of the APFloat class without just getting the float, double or quad 5556// and then using a formatted print on it which defeats the purpose. We ideally 5557// would like to get perfect string values for any kind of float semantics 5558// so we can support remote targets. The code below also requires a patch to 5559// llvm::APInt. 5560//bool 5561//ClangASTContext::ConvertFloatValueToString (ASTContext *ast, clang_type_t clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str) 5562//{ 5563// uint32_t count = 0; 5564// bool is_complex = false; 5565// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 5566// { 5567// unsigned num_bytes_per_float = byte_size / count; 5568// unsigned num_bits_per_float = num_bytes_per_float * 8; 5569// 5570// float_str.clear(); 5571// uint32_t i; 5572// for (i=0; i<count; i++) 5573// { 5574// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order); 5575// bool is_ieee = false; 5576// APFloat ap_float(ap_int, is_ieee); 5577// char s[1024]; 5578// unsigned int hex_digits = 0; 5579// bool upper_case = false; 5580// 5581// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0) 5582// { 5583// if (i > 0) 5584// float_str.append(", "); 5585// float_str.append(s); 5586// if (i == 1 && is_complex) 5587// float_str.append(1, 'i'); 5588// } 5589// } 5590// return !float_str.empty(); 5591// } 5592// return false; 5593//} 5594 5595size_t 5596ClangASTContext::ConvertStringToFloatValue (ASTContext *ast, clang_type_t clang_type, const char *s, uint8_t *dst, size_t dst_size) 5597{ 5598 if (clang_type) 5599 { 5600 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5601 uint32_t count = 0; 5602 bool is_complex = false; 5603 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 5604 { 5605 // TODO: handle complex and vector types 5606 if (count != 1) 5607 return false; 5608 5609 StringRef s_sref(s); 5610 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref); 5611 5612 const uint64_t bit_size = ast->getTypeSize (qual_type); 5613 const uint64_t byte_size = bit_size / 8; 5614 if (dst_size >= byte_size) 5615 { 5616 if (bit_size == sizeof(float)*8) 5617 { 5618 float float32 = ap_float.convertToFloat(); 5619 ::memcpy (dst, &float32, byte_size); 5620 return byte_size; 5621 } 5622 else if (bit_size >= 64) 5623 { 5624 llvm::APInt ap_int(ap_float.bitcastToAPInt()); 5625 ::memcpy (dst, ap_int.getRawData(), byte_size); 5626 return byte_size; 5627 } 5628 } 5629 } 5630 } 5631 return 0; 5632} 5633 5634unsigned 5635ClangASTContext::GetTypeQualifiers(clang_type_t clang_type) 5636{ 5637 assert (clang_type); 5638 5639 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5640 5641 return qual_type.getQualifiers().getCVRQualifiers(); 5642} 5643 5644bool 5645ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type) 5646{ 5647 if (clang_type == NULL) 5648 return false; 5649 5650 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type)); 5651} 5652 5653 5654bool 5655ClangASTContext::GetCompleteType (clang_type_t clang_type) 5656{ 5657 return ClangASTContext::GetCompleteType (getASTContext(), clang_type); 5658} 5659 5660bool 5661ClangASTContext::GetCompleteDecl (clang::ASTContext *ast, 5662 clang::Decl *decl) 5663{ 5664 if (!decl) 5665 return false; 5666 5667 ExternalASTSource *ast_source = ast->getExternalSource(); 5668 5669 if (!ast_source) 5670 return false; 5671 5672 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) 5673 { 5674 if (tag_decl->getDefinition()) 5675 return true; 5676 5677 if (!tag_decl->hasExternalLexicalStorage()) 5678 return false; 5679 5680 ast_source->CompleteType(tag_decl); 5681 5682 return !tag_decl->getTypeForDecl()->isIncompleteType(); 5683 } 5684 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) 5685 { 5686 if (!objc_interface_decl->isForwardDecl()) 5687 return true; 5688 5689 if (!objc_interface_decl->hasExternalLexicalStorage()) 5690 return false; 5691 5692 ast_source->CompleteType(objc_interface_decl); 5693 5694 return !objc_interface_decl->isForwardDecl(); 5695 } 5696 else 5697 { 5698 return false; 5699 } 5700} 5701 5702clang::DeclContext * 5703ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl) 5704{ 5705 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl); 5706} 5707 5708clang::DeclContext * 5709ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl) 5710{ 5711 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl); 5712} 5713 5714