ClangASTContext.cpp revision e6ea5fe8e76b028a0565bc01543bc15f8c120e8a
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()->getObjCIdType().getAsOpaquePtr(); 935} 936 937clang_type_t 938ClangASTContext::GetBuiltInType_objc_Class() 939{ 940 return getASTContext()->getObjCClassType().getAsOpaquePtr(); 941} 942 943clang_type_t 944ClangASTContext::GetBuiltInType_objc_selector() 945{ 946 return getASTContext()->getObjCSelType().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 1785clang::FieldDecl * 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 NULL; 1798 1799 FieldDecl *field = NULL; 1800 IdentifierTable *identifier_table = &ast->Idents; 1801 1802 assert (ast != NULL); 1803 assert (identifier_table != NULL); 1804 1805 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type)); 1806 1807 const clang::Type *clang_type = record_qual_type.getTypePtr(); 1808 if (clang_type) 1809 { 1810 const RecordType *record_type = dyn_cast<RecordType>(clang_type); 1811 1812 if (record_type) 1813 { 1814 RecordDecl *record_decl = record_type->getDecl(); 1815 1816 clang::Expr *bit_width = NULL; 1817 if (bitfield_bit_size != 0) 1818 { 1819 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size); 1820 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation()); 1821 } 1822 field = FieldDecl::Create (*ast, 1823 record_decl, 1824 SourceLocation(), 1825 SourceLocation(), 1826 name ? &identifier_table->get(name) : NULL, // Identifier 1827 QualType::getFromOpaquePtr(field_type), // Field type 1828 NULL, // TInfo * 1829 bit_width, // BitWidth 1830 false, // Mutable 1831 false); // HasInit 1832 1833 field->setAccess (ConvertAccessTypeToAccessSpecifier (access)); 1834 1835 if (field) 1836 { 1837 record_decl->addDecl(field); 1838 1839#ifdef LLDB_CONFIGURATION_DEBUG 1840 VerifyDecl(field); 1841#endif 1842 } 1843 } 1844 else 1845 { 1846 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type); 1847 if (objc_class_type) 1848 { 1849 bool is_synthesized = false; 1850 field = ClangASTContext::AddObjCClassIVar (ast, 1851 record_clang_type, 1852 name, 1853 field_type, 1854 access, 1855 bitfield_bit_size, 1856 is_synthesized); 1857 } 1858 } 1859 } 1860 return field; 1861} 1862 1863bool 1864ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size) 1865{ 1866 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size); 1867} 1868 1869bool 1870ClangASTContext::FieldIsBitfield 1871( 1872 ASTContext *ast, 1873 FieldDecl* field, 1874 uint32_t& bitfield_bit_size 1875) 1876{ 1877 if (ast == NULL || field == NULL) 1878 return false; 1879 1880 if (field->isBitField()) 1881 { 1882 Expr* bit_width_expr = field->getBitWidth(); 1883 if (bit_width_expr) 1884 { 1885 llvm::APSInt bit_width_apsint; 1886 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) 1887 { 1888 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX); 1889 return true; 1890 } 1891 } 1892 } 1893 return false; 1894} 1895 1896bool 1897ClangASTContext::RecordHasFields (const RecordDecl *record_decl) 1898{ 1899 if (record_decl == NULL) 1900 return false; 1901 1902 if (!record_decl->field_empty()) 1903 return true; 1904 1905 // No fields, lets check this is a CXX record and check the base classes 1906 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1907 if (cxx_record_decl) 1908 { 1909 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1910 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1911 base_class != base_class_end; 1912 ++base_class) 1913 { 1914 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1915 if (RecordHasFields(base_class_decl)) 1916 return true; 1917 } 1918 } 1919 return false; 1920} 1921 1922void 1923ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities) 1924{ 1925 if (clang_type) 1926 { 1927 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 1928 1929 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr()); 1930 if (record_type) 1931 { 1932 RecordDecl *record_decl = record_type->getDecl(); 1933 if (record_decl) 1934 { 1935 uint32_t field_idx; 1936 RecordDecl::field_iterator field, field_end; 1937 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0; 1938 field != field_end; 1939 ++field, ++field_idx) 1940 { 1941 // If no accessibility was assigned, assign the correct one 1942 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none) 1943 field->setAccess ((AccessSpecifier)default_accessibility); 1944 } 1945 } 1946 } 1947 } 1948} 1949 1950#pragma mark C++ Base Classes 1951 1952CXXBaseSpecifier * 1953ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class) 1954{ 1955 if (base_class_type) 1956 return new CXXBaseSpecifier (SourceRange(), 1957 is_virtual, 1958 base_of_class, 1959 ConvertAccessTypeToAccessSpecifier (access), 1960 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)), 1961 SourceLocation()); 1962 return NULL; 1963} 1964 1965void 1966ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes) 1967{ 1968 for (unsigned i=0; i<num_base_classes; ++i) 1969 { 1970 delete base_classes[i]; 1971 base_classes[i] = NULL; 1972 } 1973} 1974 1975bool 1976ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes) 1977{ 1978 if (class_clang_type) 1979 { 1980 CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl(); 1981 if (cxx_record_decl) 1982 { 1983 cxx_record_decl->setBases(base_classes, num_base_classes); 1984 return true; 1985 } 1986 } 1987 return false; 1988} 1989#pragma mark Objective C Classes 1990 1991clang_type_t 1992ClangASTContext::CreateObjCClass 1993( 1994 const char *name, 1995 DeclContext *decl_ctx, 1996 bool isForwardDecl, 1997 bool isInternal 1998) 1999{ 2000 ASTContext *ast = getASTContext(); 2001 assert (ast != NULL); 2002 assert (name && name[0]); 2003 if (decl_ctx == NULL) 2004 decl_ctx = ast->getTranslationUnitDecl(); 2005 2006 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and 2007 // we will need to update this code. I was told to currently always use 2008 // the CXXRecordDecl class since we often don't know from debug information 2009 // if something is struct or a class, so we default to always use the more 2010 // complete definition just in case. 2011 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast, 2012 decl_ctx, 2013 SourceLocation(), 2014 &ast->Idents.get(name), 2015 SourceLocation(), 2016 isForwardDecl, 2017 isInternal); 2018 2019 return ast->getObjCInterfaceType(decl).getAsOpaquePtr(); 2020} 2021 2022bool 2023ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type) 2024{ 2025 if (class_opaque_type && super_opaque_type) 2026 { 2027 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 2028 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type)); 2029 const clang::Type *class_type = class_qual_type.getTypePtr(); 2030 const clang::Type *super_type = super_qual_type.getTypePtr(); 2031 if (class_type && super_type) 2032 { 2033 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 2034 const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type); 2035 if (objc_class_type && objc_super_type) 2036 { 2037 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2038 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface(); 2039 if (class_interface_decl && super_interface_decl) 2040 { 2041 class_interface_decl->setSuperClass(super_interface_decl); 2042 return true; 2043 } 2044 } 2045 } 2046 } 2047 return false; 2048} 2049 2050 2051FieldDecl * 2052ClangASTContext::AddObjCClassIVar 2053( 2054 ASTContext *ast, 2055 clang_type_t class_opaque_type, 2056 const char *name, 2057 clang_type_t ivar_opaque_type, 2058 AccessType access, 2059 uint32_t bitfield_bit_size, 2060 bool is_synthesized 2061) 2062{ 2063 if (class_opaque_type == NULL || ivar_opaque_type == NULL) 2064 return NULL; 2065 2066 ObjCIvarDecl *field = NULL; 2067 2068 IdentifierTable *identifier_table = &ast->Idents; 2069 2070 assert (ast != NULL); 2071 assert (identifier_table != NULL); 2072 2073 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 2074 2075 const clang::Type *class_type = class_qual_type.getTypePtr(); 2076 if (class_type) 2077 { 2078 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 2079 2080 if (objc_class_type) 2081 { 2082 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2083 2084 if (class_interface_decl) 2085 { 2086 clang::Expr *bit_width = NULL; 2087 if (bitfield_bit_size != 0) 2088 { 2089 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size); 2090 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation()); 2091 } 2092 2093 field = ObjCIvarDecl::Create (*ast, 2094 class_interface_decl, 2095 SourceLocation(), 2096 SourceLocation(), 2097 &identifier_table->get(name), // Identifier 2098 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type 2099 NULL, // TypeSourceInfo * 2100 ConvertAccessTypeToObjCIvarAccessControl (access), 2101 bit_width, 2102 is_synthesized); 2103 2104 if (field) 2105 { 2106 class_interface_decl->addDecl(field); 2107 2108#ifdef LLDB_CONFIGURATION_DEBUG 2109 VerifyDecl(field); 2110#endif 2111 2112 return field; 2113 } 2114 } 2115 } 2116 } 2117 return NULL; 2118} 2119 2120bool 2121ClangASTContext::AddObjCClassProperty 2122( 2123 ASTContext *ast, 2124 clang_type_t class_opaque_type, 2125 const char *property_name, 2126 clang_type_t property_opaque_type, 2127 ObjCIvarDecl *ivar_decl, 2128 const char *property_setter_name, 2129 const char *property_getter_name, 2130 uint32_t property_attributes 2131) 2132{ 2133 if (class_opaque_type == NULL) 2134 return false; 2135 2136 IdentifierTable *identifier_table = &ast->Idents; 2137 2138 assert (ast != NULL); 2139 assert (identifier_table != NULL); 2140 2141 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 2142 const clang::Type *class_type = class_qual_type.getTypePtr(); 2143 if (class_type) 2144 { 2145 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 2146 2147 if (objc_class_type) 2148 { 2149 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2150 2151 // FIXME: For now, we don't know how to add properties if we don't have their associated ivar. 2152 if (class_interface_decl && ivar_decl) 2153 { 2154 clang::TypeSourceInfo *prop_type_source; 2155 if (ivar_decl) 2156 prop_type_source = ast->CreateTypeSourceInfo (ivar_decl->getType()); 2157 else 2158 prop_type_source = ast->CreateTypeSourceInfo (QualType::getFromOpaquePtr(property_opaque_type)); 2159 2160 ObjCPropertyDecl *property_decl = ObjCPropertyDecl::Create(*ast, 2161 class_interface_decl, 2162 SourceLocation(), // Source Location 2163 &identifier_table->get(property_name), 2164 SourceLocation(), //Source Location for AT 2165 prop_type_source 2166 ); 2167 if (property_decl) 2168 { 2169 class_interface_decl->addDecl (property_decl); 2170 if (property_setter_name != NULL) 2171 { 2172 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1); 2173 clang::IdentifierInfo *setter_ident = &identifier_table->get(property_setter_no_colon.c_str()); 2174 Selector setter_sel = ast->Selectors.getSelector(1, &setter_ident); 2175 property_decl->setSetterName(setter_sel); 2176 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter); 2177 } 2178 2179 if (property_getter_name != NULL) 2180 { 2181 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_getter_name); 2182 Selector getter_sel = ast->Selectors.getSelector(0, &getter_ident); 2183 property_decl->setGetterName(getter_sel); 2184 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter); 2185 2186 } 2187 2188 if (ivar_decl) 2189 property_decl->setPropertyIvarDecl (ivar_decl); 2190 2191 if (property_attributes & DW_APPLE_PROPERTY_readonly) 2192 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly); 2193 if (property_attributes & DW_APPLE_PROPERTY_readwrite) 2194 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite); 2195 if (property_attributes & DW_APPLE_PROPERTY_assign) 2196 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign); 2197 if (property_attributes & DW_APPLE_PROPERTY_retain) 2198 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain); 2199 if (property_attributes & DW_APPLE_PROPERTY_copy) 2200 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy); 2201 if (property_attributes & DW_APPLE_PROPERTY_nonatomic) 2202 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic); 2203 2204 return true; 2205 } 2206 } 2207 } 2208 } 2209 return false; 2210} 2211 2212bool 2213ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass) 2214{ 2215 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 2216 2217 const clang::Type *class_type = class_qual_type.getTypePtr(); 2218 if (class_type) 2219 { 2220 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 2221 2222 if (objc_class_type) 2223 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass); 2224 } 2225 return false; 2226} 2227 2228bool 2229ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass) 2230{ 2231 while (class_interface_decl) 2232 { 2233 if (class_interface_decl->ivar_size() > 0) 2234 return true; 2235 2236 if (check_superclass) 2237 class_interface_decl = class_interface_decl->getSuperClass(); 2238 else 2239 break; 2240 } 2241 return false; 2242} 2243 2244ObjCMethodDecl * 2245ClangASTContext::AddMethodToObjCObjectType 2246( 2247 ASTContext *ast, 2248 clang_type_t class_opaque_type, 2249 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]") 2250 clang_type_t method_opaque_type, 2251 lldb::AccessType access 2252) 2253{ 2254 if (class_opaque_type == NULL || method_opaque_type == NULL) 2255 return NULL; 2256 2257 IdentifierTable *identifier_table = &ast->Idents; 2258 2259 assert (ast != NULL); 2260 assert (identifier_table != NULL); 2261 2262 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 2263 2264 const clang::Type *class_type = class_qual_type.getTypePtr(); 2265 if (class_type == NULL) 2266 return NULL; 2267 2268 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 2269 2270 if (objc_class_type == NULL) 2271 return NULL; 2272 2273 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2274 2275 if (class_interface_decl == NULL) 2276 return NULL; 2277 2278 const char *selector_start = ::strchr (name, ' '); 2279 if (selector_start == NULL) 2280 return NULL; 2281 2282 selector_start++; 2283 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_')) 2284 return NULL; 2285 llvm::SmallVector<IdentifierInfo *, 12> selector_idents; 2286 2287 size_t len = 0; 2288 const char *start; 2289 //printf ("name = '%s'\n", name); 2290 2291 unsigned num_selectors_with_args = 0; 2292 for (start = selector_start; 2293 start && *start != '\0' && *start != ']'; 2294 start += len) 2295 { 2296 len = ::strcspn(start, ":]"); 2297 bool has_arg = (start[len] == ':'); 2298 if (has_arg) 2299 ++num_selectors_with_args; 2300 selector_idents.push_back (&identifier_table->get (StringRef (start, len))); 2301 if (has_arg) 2302 len += 1; 2303 } 2304 2305 2306 if (selector_idents.size() == 0) 2307 return 0; 2308 2309 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0, 2310 selector_idents.data()); 2311 2312 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type)); 2313 2314 // Populate the method decl with parameter decls 2315 const clang::Type *method_type(method_qual_type.getTypePtr()); 2316 2317 if (method_type == NULL) 2318 return NULL; 2319 2320 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type)); 2321 2322 if (!method_function_prototype) 2323 return NULL; 2324 2325 2326 bool is_variadic = false; 2327 bool is_synthesized = false; 2328 bool is_defined = false; 2329 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None; 2330 2331 const unsigned num_args = method_function_prototype->getNumArgs(); 2332 2333 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast, 2334 SourceLocation(), // beginLoc, 2335 SourceLocation(), // endLoc, 2336 method_selector, 2337 method_function_prototype->getResultType(), 2338 NULL, // TypeSourceInfo *ResultTInfo, 2339 GetDeclContextForType (class_opaque_type), 2340 name[0] == '-', 2341 is_variadic, 2342 is_synthesized, 2343 true, // is_implicitly_declared 2344 is_defined, 2345 imp_control, 2346 false /*has_related_result_type*/); 2347 2348 2349 if (objc_method_decl == NULL) 2350 return NULL; 2351 2352 if (num_args > 0) 2353 { 2354 llvm::SmallVector<ParmVarDecl *, 12> params; 2355 2356 for (int param_index = 0; param_index < num_args; ++param_index) 2357 { 2358 params.push_back (ParmVarDecl::Create (*ast, 2359 objc_method_decl, 2360 SourceLocation(), 2361 SourceLocation(), 2362 NULL, // anonymous 2363 method_function_prototype->getArgType(param_index), 2364 NULL, 2365 SC_Auto, 2366 SC_Auto, 2367 NULL)); 2368 } 2369 2370 objc_method_decl->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>()); 2371 } 2372 2373 class_interface_decl->addDecl (objc_method_decl); 2374 2375#ifdef LLDB_CONFIGURATION_DEBUG 2376 VerifyDecl(objc_method_decl); 2377#endif 2378 2379 return objc_method_decl; 2380} 2381 2382 2383uint32_t 2384ClangASTContext::GetTypeInfo 2385( 2386 clang_type_t clang_type, 2387 clang::ASTContext *ast, 2388 clang_type_t *pointee_or_element_clang_type 2389) 2390{ 2391 if (clang_type == NULL) 2392 return 0; 2393 2394 if (pointee_or_element_clang_type) 2395 *pointee_or_element_clang_type = NULL; 2396 2397 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2398 2399 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2400 switch (type_class) 2401 { 2402 case clang::Type::Builtin: 2403 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 2404 { 2405 case clang::BuiltinType::ObjCId: 2406 case clang::BuiltinType::ObjCClass: 2407 if (ast && pointee_or_element_clang_type) 2408 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr(); 2409 return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue; 2410 break; 2411 case clang::BuiltinType::Bool: 2412 case clang::BuiltinType::Char_U: 2413 case clang::BuiltinType::UChar: 2414 case clang::BuiltinType::WChar_U: 2415 case clang::BuiltinType::Char16: 2416 case clang::BuiltinType::Char32: 2417 case clang::BuiltinType::UShort: 2418 case clang::BuiltinType::UInt: 2419 case clang::BuiltinType::ULong: 2420 case clang::BuiltinType::ULongLong: 2421 case clang::BuiltinType::UInt128: 2422 case clang::BuiltinType::Char_S: 2423 case clang::BuiltinType::SChar: 2424 case clang::BuiltinType::WChar_S: 2425 case clang::BuiltinType::Short: 2426 case clang::BuiltinType::Int: 2427 case clang::BuiltinType::Long: 2428 case clang::BuiltinType::LongLong: 2429 case clang::BuiltinType::Int128: 2430 case clang::BuiltinType::Float: 2431 case clang::BuiltinType::Double: 2432 case clang::BuiltinType::LongDouble: 2433 return eTypeIsBuiltIn | eTypeHasValue | eTypeIsScalar; 2434 default: 2435 break; 2436 } 2437 return eTypeIsBuiltIn | eTypeHasValue; 2438 2439 case clang::Type::BlockPointer: 2440 if (pointee_or_element_clang_type) 2441 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr(); 2442 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock; 2443 2444 case clang::Type::Complex: return eTypeIsBuiltIn | eTypeHasValue; 2445 2446 case clang::Type::ConstantArray: 2447 case clang::Type::DependentSizedArray: 2448 case clang::Type::IncompleteArray: 2449 case clang::Type::VariableArray: 2450 if (pointee_or_element_clang_type) 2451 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr(); 2452 return eTypeHasChildren | eTypeIsArray; 2453 2454 case clang::Type::DependentName: return 0; 2455 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector; 2456 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate; 2457 case clang::Type::Decltype: return 0; 2458 2459 case clang::Type::Enum: 2460 if (pointee_or_element_clang_type) 2461 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr(); 2462 return eTypeIsEnumeration | eTypeHasValue; 2463 2464 case clang::Type::Elaborated: 2465 return ClangASTContext::GetTypeInfo (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), 2466 ast, 2467 pointee_or_element_clang_type); 2468 case clang::Type::ExtVector: return eTypeHasChildren | eTypeIsVector; 2469 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue; 2470 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue; 2471 case clang::Type::InjectedClassName: return 0; 2472 2473 case clang::Type::LValueReference: 2474 case clang::Type::RValueReference: 2475 if (pointee_or_element_clang_type) 2476 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(); 2477 return eTypeHasChildren | eTypeIsReference | eTypeHasValue; 2478 2479 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue; 2480 2481 case clang::Type::ObjCObjectPointer: 2482 if (pointee_or_element_clang_type) 2483 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr(); 2484 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue; 2485 2486 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; 2487 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; 2488 2489 case clang::Type::Pointer: 2490 if (pointee_or_element_clang_type) 2491 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr(); 2492 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue; 2493 2494 case clang::Type::Record: 2495 if (qual_type->getAsCXXRecordDecl()) 2496 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus; 2497 else 2498 return eTypeHasChildren | eTypeIsStructUnion; 2499 break; 2500 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate; 2501 case clang::Type::TemplateTypeParm: return eTypeIsTemplate; 2502 case clang::Type::TemplateSpecialization: return eTypeIsTemplate; 2503 2504 case clang::Type::Typedef: 2505 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 2506 ast, 2507 pointee_or_element_clang_type); 2508 2509 case clang::Type::TypeOfExpr: return 0; 2510 case clang::Type::TypeOf: return 0; 2511 case clang::Type::UnresolvedUsing: return 0; 2512 case clang::Type::Vector: return eTypeHasChildren | eTypeIsVector; 2513 default: return 0; 2514 } 2515 return 0; 2516} 2517 2518 2519#pragma mark Aggregate Types 2520 2521bool 2522ClangASTContext::IsAggregateType (clang_type_t clang_type) 2523{ 2524 if (clang_type == NULL) 2525 return false; 2526 2527 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2528 2529 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2530 switch (type_class) 2531 { 2532 case clang::Type::IncompleteArray: 2533 case clang::Type::VariableArray: 2534 case clang::Type::ConstantArray: 2535 case clang::Type::ExtVector: 2536 case clang::Type::Vector: 2537 case clang::Type::Record: 2538 case clang::Type::ObjCObject: 2539 case clang::Type::ObjCInterface: 2540 return true; 2541 case clang::Type::Elaborated: 2542 return ClangASTContext::IsAggregateType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 2543 case clang::Type::Typedef: 2544 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 2545 2546 default: 2547 break; 2548 } 2549 // The clang type does have a value 2550 return false; 2551} 2552 2553uint32_t 2554ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes) 2555{ 2556 if (clang_type == NULL) 2557 return 0; 2558 2559 uint32_t num_children = 0; 2560 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2561 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2562 switch (type_class) 2563 { 2564 case clang::Type::Builtin: 2565 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 2566 { 2567 case clang::BuiltinType::ObjCId: // child is Class 2568 case clang::BuiltinType::ObjCClass: // child is Class 2569 num_children = 1; 2570 break; 2571 2572 default: 2573 break; 2574 } 2575 break; 2576 2577 case clang::Type::Complex: return 0; 2578 2579 case clang::Type::Record: 2580 if (GetCompleteQualType (ast, qual_type)) 2581 { 2582 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 2583 const RecordDecl *record_decl = record_type->getDecl(); 2584 assert(record_decl); 2585 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 2586 if (cxx_record_decl) 2587 { 2588 if (omit_empty_base_classes) 2589 { 2590 // Check each base classes to see if it or any of its 2591 // base classes contain any fields. This can help 2592 // limit the noise in variable views by not having to 2593 // show base classes that contain no members. 2594 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2595 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2596 base_class != base_class_end; 2597 ++base_class) 2598 { 2599 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2600 2601 // Skip empty base classes 2602 if (RecordHasFields(base_class_decl) == false) 2603 continue; 2604 2605 num_children++; 2606 } 2607 } 2608 else 2609 { 2610 // Include all base classes 2611 num_children += cxx_record_decl->getNumBases(); 2612 } 2613 2614 } 2615 RecordDecl::field_iterator field, field_end; 2616 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field) 2617 ++num_children; 2618 } 2619 break; 2620 2621 case clang::Type::ObjCObject: 2622 case clang::Type::ObjCInterface: 2623 if (GetCompleteQualType (ast, qual_type)) 2624 { 2625 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 2626 assert (objc_class_type); 2627 if (objc_class_type) 2628 { 2629 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2630 2631 if (class_interface_decl) 2632 { 2633 2634 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 2635 if (superclass_interface_decl) 2636 { 2637 if (omit_empty_base_classes) 2638 { 2639 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true)) 2640 ++num_children; 2641 } 2642 else 2643 ++num_children; 2644 } 2645 2646 num_children += class_interface_decl->ivar_size(); 2647 } 2648 } 2649 } 2650 break; 2651 2652 case clang::Type::ObjCObjectPointer: 2653 { 2654 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr()); 2655 QualType pointee_type = pointer_type->getPointeeType(); 2656 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast, 2657 pointee_type.getAsOpaquePtr(), 2658 omit_empty_base_classes); 2659 // If this type points to a simple type, then it has 1 child 2660 if (num_pointee_children == 0) 2661 num_children = 1; 2662 else 2663 num_children = num_pointee_children; 2664 } 2665 break; 2666 2667 case clang::Type::ConstantArray: 2668 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue(); 2669 break; 2670 2671 case clang::Type::Pointer: 2672 { 2673 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 2674 QualType pointee_type (pointer_type->getPointeeType()); 2675 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast, 2676 pointee_type.getAsOpaquePtr(), 2677 omit_empty_base_classes); 2678 if (num_pointee_children == 0) 2679 { 2680 // We have a pointer to a pointee type that claims it has no children. 2681 // We will want to look at 2682 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr()); 2683 } 2684 else 2685 num_children = num_pointee_children; 2686 } 2687 break; 2688 2689 case clang::Type::LValueReference: 2690 case clang::Type::RValueReference: 2691 { 2692 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 2693 QualType pointee_type = reference_type->getPointeeType(); 2694 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast, 2695 pointee_type.getAsOpaquePtr(), 2696 omit_empty_base_classes); 2697 // If this type points to a simple type, then it has 1 child 2698 if (num_pointee_children == 0) 2699 num_children = 1; 2700 else 2701 num_children = num_pointee_children; 2702 } 2703 break; 2704 2705 2706 case clang::Type::Typedef: 2707 num_children = ClangASTContext::GetNumChildren (ast, 2708 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 2709 omit_empty_base_classes); 2710 break; 2711 2712 case clang::Type::Elaborated: 2713 num_children = ClangASTContext::GetNumChildren (ast, 2714 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), 2715 omit_empty_base_classes); 2716 break; 2717 2718 default: 2719 break; 2720 } 2721 return num_children; 2722} 2723 2724uint32_t 2725ClangASTContext::GetNumDirectBaseClasses (clang::ASTContext *ast, clang_type_t clang_type) 2726{ 2727 if (clang_type == NULL) 2728 return 0; 2729 2730 uint32_t count = 0; 2731 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2732 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2733 switch (type_class) 2734 { 2735 case clang::Type::Record: 2736 if (GetCompleteQualType (ast, qual_type)) 2737 { 2738 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 2739 if (cxx_record_decl) 2740 count = cxx_record_decl->getNumBases(); 2741 } 2742 break; 2743 2744 case clang::Type::ObjCObject: 2745 case clang::Type::ObjCInterface: 2746 if (GetCompleteQualType (ast, qual_type)) 2747 { 2748 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType(); 2749 if (objc_class_type) 2750 { 2751 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2752 2753 if (class_interface_decl && class_interface_decl->getSuperClass()) 2754 count = 1; 2755 } 2756 } 2757 break; 2758 2759 2760 case clang::Type::Typedef: 2761 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 2762 break; 2763 2764 case clang::Type::Elaborated: 2765 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 2766 break; 2767 2768 default: 2769 break; 2770 } 2771 return count; 2772} 2773 2774uint32_t 2775ClangASTContext::GetNumVirtualBaseClasses (clang::ASTContext *ast, 2776 clang_type_t clang_type) 2777{ 2778 if (clang_type == NULL) 2779 return 0; 2780 2781 uint32_t count = 0; 2782 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2783 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2784 switch (type_class) 2785 { 2786 case clang::Type::Record: 2787 if (GetCompleteQualType (ast, qual_type)) 2788 { 2789 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 2790 if (cxx_record_decl) 2791 count = cxx_record_decl->getNumVBases(); 2792 } 2793 break; 2794 2795 case clang::Type::Typedef: 2796 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 2797 break; 2798 2799 case clang::Type::Elaborated: 2800 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 2801 break; 2802 2803 default: 2804 break; 2805 } 2806 return count; 2807} 2808 2809uint32_t 2810ClangASTContext::GetNumFields (clang::ASTContext *ast, clang_type_t clang_type) 2811{ 2812 if (clang_type == NULL) 2813 return 0; 2814 2815 uint32_t count = 0; 2816 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2817 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2818 switch (type_class) 2819 { 2820 case clang::Type::Record: 2821 if (GetCompleteQualType (ast, qual_type)) 2822 { 2823 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr()); 2824 if (record_type) 2825 { 2826 RecordDecl *record_decl = record_type->getDecl(); 2827 if (record_decl) 2828 { 2829 uint32_t field_idx = 0; 2830 RecordDecl::field_iterator field, field_end; 2831 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field) 2832 ++field_idx; 2833 count = field_idx; 2834 } 2835 } 2836 } 2837 break; 2838 2839 case clang::Type::Typedef: 2840 count = ClangASTContext::GetNumFields (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 2841 break; 2842 2843 case clang::Type::Elaborated: 2844 count = ClangASTContext::GetNumFields (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 2845 break; 2846 2847 case clang::Type::ObjCObject: 2848 case clang::Type::ObjCInterface: 2849 if (GetCompleteQualType (ast, qual_type)) 2850 { 2851 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 2852 if (objc_class_type) 2853 { 2854 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2855 2856 if (class_interface_decl) 2857 count = class_interface_decl->ivar_size(); 2858 } 2859 } 2860 break; 2861 2862 default: 2863 break; 2864 } 2865 return count; 2866} 2867 2868clang_type_t 2869ClangASTContext::GetDirectBaseClassAtIndex (clang::ASTContext *ast, 2870 clang_type_t clang_type, 2871 uint32_t idx, 2872 uint32_t *bit_offset_ptr) 2873{ 2874 if (clang_type == NULL) 2875 return 0; 2876 2877 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2878 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2879 switch (type_class) 2880 { 2881 case clang::Type::Record: 2882 if (GetCompleteQualType (ast, qual_type)) 2883 { 2884 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 2885 if (cxx_record_decl) 2886 { 2887 uint32_t curr_idx = 0; 2888 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2889 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2890 base_class != base_class_end; 2891 ++base_class, ++curr_idx) 2892 { 2893 if (curr_idx == idx) 2894 { 2895 if (bit_offset_ptr) 2896 { 2897 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl); 2898 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2899// if (base_class->isVirtual()) 2900// *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8; 2901// else 2902 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8; 2903 } 2904 return base_class->getType().getAsOpaquePtr(); 2905 } 2906 } 2907 } 2908 } 2909 break; 2910 2911 case clang::Type::ObjCObject: 2912 case clang::Type::ObjCInterface: 2913 if (idx == 0 && GetCompleteQualType (ast, qual_type)) 2914 { 2915 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType(); 2916 if (objc_class_type) 2917 { 2918 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2919 2920 if (class_interface_decl) 2921 { 2922 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 2923 if (superclass_interface_decl) 2924 { 2925 if (bit_offset_ptr) 2926 *bit_offset_ptr = 0; 2927 return ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(); 2928 } 2929 } 2930 } 2931 } 2932 break; 2933 2934 2935 case clang::Type::Typedef: 2936 return ClangASTContext::GetDirectBaseClassAtIndex (ast, 2937 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 2938 idx, 2939 bit_offset_ptr); 2940 2941 case clang::Type::Elaborated: 2942 return ClangASTContext::GetDirectBaseClassAtIndex (ast, 2943 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), 2944 idx, 2945 bit_offset_ptr); 2946 2947 default: 2948 break; 2949 } 2950 return NULL; 2951} 2952 2953clang_type_t 2954ClangASTContext::GetVirtualBaseClassAtIndex (clang::ASTContext *ast, 2955 clang_type_t clang_type, 2956 uint32_t idx, 2957 uint32_t *bit_offset_ptr) 2958{ 2959 if (clang_type == NULL) 2960 return 0; 2961 2962 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2963 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2964 switch (type_class) 2965 { 2966 case clang::Type::Record: 2967 if (GetCompleteQualType (ast, qual_type)) 2968 { 2969 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 2970 if (cxx_record_decl) 2971 { 2972 uint32_t curr_idx = 0; 2973 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2974 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end(); 2975 base_class != base_class_end; 2976 ++base_class, ++curr_idx) 2977 { 2978 if (curr_idx == idx) 2979 { 2980 if (bit_offset_ptr) 2981 { 2982 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl); 2983 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2984 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8; 2985 2986 } 2987 return base_class->getType().getAsOpaquePtr(); 2988 } 2989 } 2990 } 2991 } 2992 break; 2993 2994 case clang::Type::Typedef: 2995 return ClangASTContext::GetVirtualBaseClassAtIndex (ast, 2996 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 2997 idx, 2998 bit_offset_ptr); 2999 3000 case clang::Type::Elaborated: 3001 return ClangASTContext::GetVirtualBaseClassAtIndex (ast, 3002 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), 3003 idx, 3004 bit_offset_ptr); 3005 3006 default: 3007 break; 3008 } 3009 return NULL; 3010} 3011 3012clang_type_t 3013ClangASTContext::GetFieldAtIndex (clang::ASTContext *ast, 3014 clang_type_t clang_type, 3015 uint32_t idx, 3016 std::string& name, 3017 uint32_t *bit_offset_ptr) 3018{ 3019 if (clang_type == NULL) 3020 return 0; 3021 3022 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 3023 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3024 switch (type_class) 3025 { 3026 case clang::Type::Record: 3027 if (GetCompleteQualType (ast, qual_type)) 3028 { 3029 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 3030 const RecordDecl *record_decl = record_type->getDecl(); 3031 uint32_t field_idx = 0; 3032 RecordDecl::field_iterator field, field_end; 3033 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx) 3034 { 3035 if (idx == field_idx) 3036 { 3037 // Print the member type if requested 3038 // Print the member name and equal sign 3039 name.assign(field->getNameAsString()); 3040 3041 // Figure out the type byte size (field_type_info.first) and 3042 // alignment (field_type_info.second) from the AST context. 3043 if (bit_offset_ptr) 3044 { 3045 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl); 3046 *bit_offset_ptr = record_layout.getFieldOffset (field_idx); 3047 } 3048 3049 return field->getType().getAsOpaquePtr(); 3050 } 3051 } 3052 } 3053 break; 3054 3055 case clang::Type::ObjCObject: 3056 case clang::Type::ObjCInterface: 3057 if (GetCompleteQualType (ast, qual_type)) 3058 { 3059 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 3060 assert (objc_class_type); 3061 if (objc_class_type) 3062 { 3063 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 3064 3065 if (class_interface_decl) 3066 { 3067 if (idx < (class_interface_decl->ivar_size())) 3068 { 3069 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 3070 uint32_t ivar_idx = 0; 3071 3072 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx) 3073 { 3074 if (ivar_idx == idx) 3075 { 3076 const ObjCIvarDecl* ivar_decl = *ivar_pos; 3077 3078 QualType ivar_qual_type(ivar_decl->getType()); 3079 3080 name.assign(ivar_decl->getNameAsString()); 3081 3082 if (bit_offset_ptr) 3083 { 3084 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl); 3085 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx); 3086 } 3087 3088 return ivar_qual_type.getAsOpaquePtr(); 3089 } 3090 } 3091 } 3092 } 3093 } 3094 } 3095 break; 3096 3097 3098 case clang::Type::Typedef: 3099 return ClangASTContext::GetFieldAtIndex (ast, 3100 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 3101 idx, 3102 name, 3103 bit_offset_ptr); 3104 3105 case clang::Type::Elaborated: 3106 return ClangASTContext::GetFieldAtIndex (ast, 3107 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), 3108 idx, 3109 name, 3110 bit_offset_ptr); 3111 3112 default: 3113 break; 3114 } 3115 return NULL; 3116} 3117 3118 3119// If a pointer to a pointee type (the clang_type arg) says that it has no 3120// children, then we either need to trust it, or override it and return a 3121// different result. For example, an "int *" has one child that is an integer, 3122// but a function pointer doesn't have any children. Likewise if a Record type 3123// claims it has no children, then there really is nothing to show. 3124uint32_t 3125ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type) 3126{ 3127 if (clang_type == NULL) 3128 return 0; 3129 3130 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 3131 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3132 switch (type_class) 3133 { 3134 case clang::Type::Builtin: 3135 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 3136 { 3137 case clang::BuiltinType::UnknownAny: 3138 case clang::BuiltinType::Void: 3139 case clang::BuiltinType::NullPtr: 3140 return 0; 3141 case clang::BuiltinType::Bool: 3142 case clang::BuiltinType::Char_U: 3143 case clang::BuiltinType::UChar: 3144 case clang::BuiltinType::WChar_U: 3145 case clang::BuiltinType::Char16: 3146 case clang::BuiltinType::Char32: 3147 case clang::BuiltinType::UShort: 3148 case clang::BuiltinType::UInt: 3149 case clang::BuiltinType::ULong: 3150 case clang::BuiltinType::ULongLong: 3151 case clang::BuiltinType::UInt128: 3152 case clang::BuiltinType::Char_S: 3153 case clang::BuiltinType::SChar: 3154 case clang::BuiltinType::WChar_S: 3155 case clang::BuiltinType::Short: 3156 case clang::BuiltinType::Int: 3157 case clang::BuiltinType::Long: 3158 case clang::BuiltinType::LongLong: 3159 case clang::BuiltinType::Int128: 3160 case clang::BuiltinType::Float: 3161 case clang::BuiltinType::Double: 3162 case clang::BuiltinType::LongDouble: 3163 case clang::BuiltinType::Dependent: 3164 case clang::BuiltinType::Overload: 3165 case clang::BuiltinType::ObjCId: 3166 case clang::BuiltinType::ObjCClass: 3167 case clang::BuiltinType::ObjCSel: 3168 case clang::BuiltinType::BoundMember: 3169 case clang::BuiltinType::Half: 3170 case clang::BuiltinType::ARCUnbridgedCast: 3171 case clang::BuiltinType::PseudoObject: 3172 return 1; 3173 } 3174 break; 3175 3176 case clang::Type::Complex: return 1; 3177 case clang::Type::Pointer: return 1; 3178 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them 3179 case clang::Type::LValueReference: return 1; 3180 case clang::Type::RValueReference: return 1; 3181 case clang::Type::MemberPointer: return 0; 3182 case clang::Type::ConstantArray: return 0; 3183 case clang::Type::IncompleteArray: return 0; 3184 case clang::Type::VariableArray: return 0; 3185 case clang::Type::DependentSizedArray: return 0; 3186 case clang::Type::DependentSizedExtVector: return 0; 3187 case clang::Type::Vector: return 0; 3188 case clang::Type::ExtVector: return 0; 3189 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children... 3190 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children... 3191 case clang::Type::UnresolvedUsing: return 0; 3192 case clang::Type::Paren: return 0; 3193 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 3194 case clang::Type::Elaborated: return ClangASTContext::GetNumPointeeChildren (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 3195 case clang::Type::TypeOfExpr: return 0; 3196 case clang::Type::TypeOf: return 0; 3197 case clang::Type::Decltype: return 0; 3198 case clang::Type::Record: return 0; 3199 case clang::Type::Enum: return 1; 3200 case clang::Type::TemplateTypeParm: return 1; 3201 case clang::Type::SubstTemplateTypeParm: return 1; 3202 case clang::Type::TemplateSpecialization: return 1; 3203 case clang::Type::InjectedClassName: return 0; 3204 case clang::Type::DependentName: return 1; 3205 case clang::Type::DependentTemplateSpecialization: return 1; 3206 case clang::Type::ObjCObject: return 0; 3207 case clang::Type::ObjCInterface: return 0; 3208 case clang::Type::ObjCObjectPointer: return 1; 3209 default: 3210 break; 3211 } 3212 return 0; 3213} 3214 3215clang_type_t 3216ClangASTContext::GetChildClangTypeAtIndex 3217( 3218 ExecutionContext *exe_ctx, 3219 const char *parent_name, 3220 clang_type_t parent_clang_type, 3221 uint32_t idx, 3222 bool transparent_pointers, 3223 bool omit_empty_base_classes, 3224 bool ignore_array_bounds, 3225 std::string& child_name, 3226 uint32_t &child_byte_size, 3227 int32_t &child_byte_offset, 3228 uint32_t &child_bitfield_bit_size, 3229 uint32_t &child_bitfield_bit_offset, 3230 bool &child_is_base_class, 3231 bool &child_is_deref_of_parent 3232) 3233{ 3234 if (parent_clang_type) 3235 3236 return GetChildClangTypeAtIndex (exe_ctx, 3237 getASTContext(), 3238 parent_name, 3239 parent_clang_type, 3240 idx, 3241 transparent_pointers, 3242 omit_empty_base_classes, 3243 ignore_array_bounds, 3244 child_name, 3245 child_byte_size, 3246 child_byte_offset, 3247 child_bitfield_bit_size, 3248 child_bitfield_bit_offset, 3249 child_is_base_class, 3250 child_is_deref_of_parent); 3251 return NULL; 3252} 3253 3254clang_type_t 3255ClangASTContext::GetChildClangTypeAtIndex 3256( 3257 ExecutionContext *exe_ctx, 3258 ASTContext *ast, 3259 const char *parent_name, 3260 clang_type_t parent_clang_type, 3261 uint32_t idx, 3262 bool transparent_pointers, 3263 bool omit_empty_base_classes, 3264 bool ignore_array_bounds, 3265 std::string& child_name, 3266 uint32_t &child_byte_size, 3267 int32_t &child_byte_offset, 3268 uint32_t &child_bitfield_bit_size, 3269 uint32_t &child_bitfield_bit_offset, 3270 bool &child_is_base_class, 3271 bool &child_is_deref_of_parent 3272) 3273{ 3274 if (parent_clang_type == NULL) 3275 return NULL; 3276 3277 if (idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes)) 3278 { 3279 uint32_t bit_offset; 3280 child_bitfield_bit_size = 0; 3281 child_bitfield_bit_offset = 0; 3282 child_is_base_class = false; 3283 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type)); 3284 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass(); 3285 switch (parent_type_class) 3286 { 3287 case clang::Type::Builtin: 3288 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind()) 3289 { 3290 case clang::BuiltinType::ObjCId: 3291 case clang::BuiltinType::ObjCClass: 3292 child_name = "isa"; 3293 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT; 3294 return ast->ObjCBuiltinClassTy.getAsOpaquePtr(); 3295 3296 default: 3297 break; 3298 } 3299 break; 3300 3301 case clang::Type::Record: 3302 if (GetCompleteQualType (ast, parent_qual_type)) 3303 { 3304 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr()); 3305 const RecordDecl *record_decl = record_type->getDecl(); 3306 assert(record_decl); 3307 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl); 3308 uint32_t child_idx = 0; 3309 3310 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 3311 if (cxx_record_decl) 3312 { 3313 // We might have base classes to print out first 3314 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 3315 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 3316 base_class != base_class_end; 3317 ++base_class) 3318 { 3319 const CXXRecordDecl *base_class_decl = NULL; 3320 3321 // Skip empty base classes 3322 if (omit_empty_base_classes) 3323 { 3324 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 3325 if (RecordHasFields(base_class_decl) == false) 3326 continue; 3327 } 3328 3329 if (idx == child_idx) 3330 { 3331 if (base_class_decl == NULL) 3332 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 3333 3334 3335 if (base_class->isVirtual()) 3336 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8; 3337 else 3338 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8; 3339 3340 // Base classes should be a multiple of 8 bits in size 3341 child_byte_offset = bit_offset/8; 3342 3343 child_name = ClangASTType::GetTypeNameForQualType(base_class->getType()); 3344 3345 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType()); 3346 3347 // Base classes bit sizes should be a multiple of 8 bits in size 3348 assert (clang_type_info_bit_size % 8 == 0); 3349 child_byte_size = clang_type_info_bit_size / 8; 3350 child_is_base_class = true; 3351 return base_class->getType().getAsOpaquePtr(); 3352 } 3353 // We don't increment the child index in the for loop since we might 3354 // be skipping empty base classes 3355 ++child_idx; 3356 } 3357 } 3358 // Make sure index is in range... 3359 uint32_t field_idx = 0; 3360 RecordDecl::field_iterator field, field_end; 3361 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx) 3362 { 3363 if (idx == child_idx) 3364 { 3365 // Print the member type if requested 3366 // Print the member name and equal sign 3367 child_name.assign(field->getNameAsString().c_str()); 3368 3369 // Figure out the type byte size (field_type_info.first) and 3370 // alignment (field_type_info.second) from the AST context. 3371 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType()); 3372 assert(field_idx < record_layout.getFieldCount()); 3373 3374 child_byte_size = field_type_info.first / 8; 3375 3376 // Figure out the field offset within the current struct/union/class type 3377 bit_offset = record_layout.getFieldOffset (field_idx); 3378 child_byte_offset = bit_offset / 8; 3379 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size)) 3380 child_bitfield_bit_offset = bit_offset % 8; 3381 3382 return field->getType().getAsOpaquePtr(); 3383 } 3384 } 3385 } 3386 break; 3387 3388 case clang::Type::ObjCObject: 3389 case clang::Type::ObjCInterface: 3390 if (GetCompleteQualType (ast, parent_qual_type)) 3391 { 3392 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr()); 3393 assert (objc_class_type); 3394 if (objc_class_type) 3395 { 3396 uint32_t child_idx = 0; 3397 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 3398 3399 if (class_interface_decl) 3400 { 3401 3402 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl); 3403 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 3404 if (superclass_interface_decl) 3405 { 3406 if (omit_empty_base_classes) 3407 { 3408 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0) 3409 { 3410 if (idx == 0) 3411 { 3412 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl)); 3413 3414 3415 child_name.assign(superclass_interface_decl->getNameAsString().c_str()); 3416 3417 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr()); 3418 3419 child_byte_size = ivar_type_info.first / 8; 3420 child_byte_offset = 0; 3421 child_is_base_class = true; 3422 3423 return ivar_qual_type.getAsOpaquePtr(); 3424 } 3425 3426 ++child_idx; 3427 } 3428 } 3429 else 3430 ++child_idx; 3431 } 3432 3433 const uint32_t superclass_idx = child_idx; 3434 3435 if (idx < (child_idx + class_interface_decl->ivar_size())) 3436 { 3437 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 3438 3439 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos) 3440 { 3441 if (child_idx == idx) 3442 { 3443 const ObjCIvarDecl* ivar_decl = *ivar_pos; 3444 3445 QualType ivar_qual_type(ivar_decl->getType()); 3446 3447 child_name.assign(ivar_decl->getNameAsString().c_str()); 3448 3449 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr()); 3450 3451 child_byte_size = ivar_type_info.first / 8; 3452 3453 // Figure out the field offset within the current struct/union/class type 3454 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since 3455 // that doesn't account for the space taken up by unbacked properties, or from 3456 // the changing size of base classes that are newer than this class. 3457 // So if we have a process around that we can ask about this object, do so. 3458 child_byte_offset = LLDB_INVALID_IVAR_OFFSET; 3459 Process *process = NULL; 3460 if (exe_ctx) 3461 process = exe_ctx->GetProcessPtr(); 3462 if (process) 3463 { 3464 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime(); 3465 if (objc_runtime != NULL) 3466 { 3467 ClangASTType parent_ast_type (ast, parent_qual_type.getAsOpaquePtr()); 3468 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str()); 3469 } 3470 } 3471 3472 if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET) 3473 { 3474 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx); 3475 child_byte_offset = bit_offset / 8; 3476 } 3477 3478 return ivar_qual_type.getAsOpaquePtr(); 3479 } 3480 ++child_idx; 3481 } 3482 } 3483 } 3484 } 3485 } 3486 break; 3487 3488 case clang::Type::ObjCObjectPointer: 3489 { 3490 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr()); 3491 QualType pointee_type = pointer_type->getPointeeType(); 3492 3493 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3494 { 3495 child_is_deref_of_parent = false; 3496 bool tmp_child_is_deref_of_parent = false; 3497 return GetChildClangTypeAtIndex (exe_ctx, 3498 ast, 3499 parent_name, 3500 pointer_type->getPointeeType().getAsOpaquePtr(), 3501 idx, 3502 transparent_pointers, 3503 omit_empty_base_classes, 3504 ignore_array_bounds, 3505 child_name, 3506 child_byte_size, 3507 child_byte_offset, 3508 child_bitfield_bit_size, 3509 child_bitfield_bit_offset, 3510 child_is_base_class, 3511 tmp_child_is_deref_of_parent); 3512 } 3513 else 3514 { 3515 child_is_deref_of_parent = true; 3516 if (parent_name) 3517 { 3518 child_name.assign(1, '*'); 3519 child_name += parent_name; 3520 } 3521 3522 // We have a pointer to an simple type 3523 if (idx == 0) 3524 { 3525 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 3526 assert(clang_type_info.first % 8 == 0); 3527 child_byte_size = clang_type_info.first / 8; 3528 child_byte_offset = 0; 3529 return pointee_type.getAsOpaquePtr(); 3530 } 3531 } 3532 } 3533 break; 3534 3535 case clang::Type::ConstantArray: 3536 { 3537 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 3538 const uint64_t element_count = array->getSize().getLimitedValue(); 3539 3540 if (ignore_array_bounds || idx < element_count) 3541 { 3542 if (GetCompleteQualType (ast, array->getElementType())) 3543 { 3544 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType()); 3545 3546 char element_name[64]; 3547 ::snprintf (element_name, sizeof (element_name), "[%u]", idx); 3548 3549 child_name.assign(element_name); 3550 assert(field_type_info.first % 8 == 0); 3551 child_byte_size = field_type_info.first / 8; 3552 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; 3553 return array->getElementType().getAsOpaquePtr(); 3554 } 3555 } 3556 } 3557 break; 3558 3559 case clang::Type::Pointer: 3560 { 3561 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr()); 3562 QualType pointee_type = pointer_type->getPointeeType(); 3563 3564 // Don't dereference "void *" pointers 3565 if (pointee_type->isVoidType()) 3566 return NULL; 3567 3568 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3569 { 3570 child_is_deref_of_parent = false; 3571 bool tmp_child_is_deref_of_parent = false; 3572 return GetChildClangTypeAtIndex (exe_ctx, 3573 ast, 3574 parent_name, 3575 pointer_type->getPointeeType().getAsOpaquePtr(), 3576 idx, 3577 transparent_pointers, 3578 omit_empty_base_classes, 3579 ignore_array_bounds, 3580 child_name, 3581 child_byte_size, 3582 child_byte_offset, 3583 child_bitfield_bit_size, 3584 child_bitfield_bit_offset, 3585 child_is_base_class, 3586 tmp_child_is_deref_of_parent); 3587 } 3588 else 3589 { 3590 child_is_deref_of_parent = true; 3591 3592 if (parent_name) 3593 { 3594 child_name.assign(1, '*'); 3595 child_name += parent_name; 3596 } 3597 3598 // We have a pointer to an simple type 3599 if (idx == 0) 3600 { 3601 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 3602 assert(clang_type_info.first % 8 == 0); 3603 child_byte_size = clang_type_info.first / 8; 3604 child_byte_offset = 0; 3605 return pointee_type.getAsOpaquePtr(); 3606 } 3607 } 3608 } 3609 break; 3610 3611 case clang::Type::LValueReference: 3612 case clang::Type::RValueReference: 3613 { 3614 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr()); 3615 QualType pointee_type(reference_type->getPointeeType()); 3616 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr(); 3617 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type)) 3618 { 3619 child_is_deref_of_parent = false; 3620 bool tmp_child_is_deref_of_parent = false; 3621 return GetChildClangTypeAtIndex (exe_ctx, 3622 ast, 3623 parent_name, 3624 pointee_clang_type, 3625 idx, 3626 transparent_pointers, 3627 omit_empty_base_classes, 3628 ignore_array_bounds, 3629 child_name, 3630 child_byte_size, 3631 child_byte_offset, 3632 child_bitfield_bit_size, 3633 child_bitfield_bit_offset, 3634 child_is_base_class, 3635 tmp_child_is_deref_of_parent); 3636 } 3637 else 3638 { 3639 if (parent_name) 3640 { 3641 child_name.assign(1, '&'); 3642 child_name += parent_name; 3643 } 3644 3645 // We have a pointer to an simple type 3646 if (idx == 0) 3647 { 3648 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 3649 assert(clang_type_info.first % 8 == 0); 3650 child_byte_size = clang_type_info.first / 8; 3651 child_byte_offset = 0; 3652 return pointee_type.getAsOpaquePtr(); 3653 } 3654 } 3655 } 3656 break; 3657 3658 case clang::Type::Typedef: 3659 return GetChildClangTypeAtIndex (exe_ctx, 3660 ast, 3661 parent_name, 3662 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 3663 idx, 3664 transparent_pointers, 3665 omit_empty_base_classes, 3666 ignore_array_bounds, 3667 child_name, 3668 child_byte_size, 3669 child_byte_offset, 3670 child_bitfield_bit_size, 3671 child_bitfield_bit_offset, 3672 child_is_base_class, 3673 child_is_deref_of_parent); 3674 break; 3675 3676 case clang::Type::Elaborated: 3677 return GetChildClangTypeAtIndex (exe_ctx, 3678 ast, 3679 parent_name, 3680 cast<ElaboratedType>(parent_qual_type)->getNamedType().getAsOpaquePtr(), 3681 idx, 3682 transparent_pointers, 3683 omit_empty_base_classes, 3684 ignore_array_bounds, 3685 child_name, 3686 child_byte_size, 3687 child_byte_offset, 3688 child_bitfield_bit_size, 3689 child_bitfield_bit_offset, 3690 child_is_base_class, 3691 child_is_deref_of_parent); 3692 3693 default: 3694 break; 3695 } 3696 } 3697 return NULL; 3698} 3699 3700static inline bool 3701BaseSpecifierIsEmpty (const CXXBaseSpecifier *b) 3702{ 3703 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false; 3704} 3705 3706static uint32_t 3707GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes) 3708{ 3709 uint32_t num_bases = 0; 3710 if (cxx_record_decl) 3711 { 3712 if (omit_empty_base_classes) 3713 { 3714 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 3715 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 3716 base_class != base_class_end; 3717 ++base_class) 3718 { 3719 // Skip empty base classes 3720 if (omit_empty_base_classes) 3721 { 3722 if (BaseSpecifierIsEmpty (base_class)) 3723 continue; 3724 } 3725 ++num_bases; 3726 } 3727 } 3728 else 3729 num_bases = cxx_record_decl->getNumBases(); 3730 } 3731 return num_bases; 3732} 3733 3734 3735static uint32_t 3736GetIndexForRecordBase 3737( 3738 const RecordDecl *record_decl, 3739 const CXXBaseSpecifier *base_spec, 3740 bool omit_empty_base_classes 3741) 3742{ 3743 uint32_t child_idx = 0; 3744 3745 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 3746 3747// const char *super_name = record_decl->getNameAsCString(); 3748// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString(); 3749// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name); 3750// 3751 if (cxx_record_decl) 3752 { 3753 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 3754 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 3755 base_class != base_class_end; 3756 ++base_class) 3757 { 3758 if (omit_empty_base_classes) 3759 { 3760 if (BaseSpecifierIsEmpty (base_class)) 3761 continue; 3762 } 3763 3764// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name, 3765// child_idx, 3766// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); 3767// 3768// 3769 if (base_class == base_spec) 3770 return child_idx; 3771 ++child_idx; 3772 } 3773 } 3774 3775 return UINT32_MAX; 3776} 3777 3778 3779static uint32_t 3780GetIndexForRecordChild 3781( 3782 const RecordDecl *record_decl, 3783 NamedDecl *canonical_decl, 3784 bool omit_empty_base_classes 3785) 3786{ 3787 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes); 3788 3789// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 3790// 3791//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString()); 3792// if (cxx_record_decl) 3793// { 3794// CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 3795// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 3796// base_class != base_class_end; 3797// ++base_class) 3798// { 3799// if (omit_empty_base_classes) 3800// { 3801// if (BaseSpecifierIsEmpty (base_class)) 3802// continue; 3803// } 3804// 3805//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", 3806//// record_decl->getNameAsCString(), 3807//// canonical_decl->getNameAsCString(), 3808//// child_idx, 3809//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); 3810// 3811// 3812// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 3813// if (curr_base_class_decl == canonical_decl) 3814// { 3815// return child_idx; 3816// } 3817// ++child_idx; 3818// } 3819// } 3820// 3821// const uint32_t num_bases = child_idx; 3822 RecordDecl::field_iterator field, field_end; 3823 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 3824 field != field_end; 3825 ++field, ++child_idx) 3826 { 3827// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n", 3828// record_decl->getNameAsCString(), 3829// canonical_decl->getNameAsCString(), 3830// child_idx - num_bases, 3831// field->getNameAsCString()); 3832 3833 if (field->getCanonicalDecl() == canonical_decl) 3834 return child_idx; 3835 } 3836 3837 return UINT32_MAX; 3838} 3839 3840// Look for a child member (doesn't include base classes, but it does include 3841// their members) in the type hierarchy. Returns an index path into "clang_type" 3842// on how to reach the appropriate member. 3843// 3844// class A 3845// { 3846// public: 3847// int m_a; 3848// int m_b; 3849// }; 3850// 3851// class B 3852// { 3853// }; 3854// 3855// class C : 3856// public B, 3857// public A 3858// { 3859// }; 3860// 3861// If we have a clang type that describes "class C", and we wanted to looked 3862// "m_b" in it: 3863// 3864// With omit_empty_base_classes == false we would get an integer array back with: 3865// { 1, 1 } 3866// The first index 1 is the child index for "class A" within class C 3867// The second index 1 is the child index for "m_b" within class A 3868// 3869// With omit_empty_base_classes == true we would get an integer array back with: 3870// { 0, 1 } 3871// 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) 3872// The second index 1 is the child index for "m_b" within class A 3873 3874size_t 3875ClangASTContext::GetIndexOfChildMemberWithName 3876( 3877 ASTContext *ast, 3878 clang_type_t clang_type, 3879 const char *name, 3880 bool omit_empty_base_classes, 3881 std::vector<uint32_t>& child_indexes 3882) 3883{ 3884 if (clang_type && name && name[0]) 3885 { 3886 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 3887 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3888 switch (type_class) 3889 { 3890 case clang::Type::Record: 3891 if (GetCompleteQualType (ast, qual_type)) 3892 { 3893 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 3894 const RecordDecl *record_decl = record_type->getDecl(); 3895 3896 assert(record_decl); 3897 uint32_t child_idx = 0; 3898 3899 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 3900 3901 // Try and find a field that matches NAME 3902 RecordDecl::field_iterator field, field_end; 3903 StringRef name_sref(name); 3904 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 3905 field != field_end; 3906 ++field, ++child_idx) 3907 { 3908 if (field->getName().equals (name_sref)) 3909 { 3910 // We have to add on the number of base classes to this index! 3911 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes)); 3912 return child_indexes.size(); 3913 } 3914 } 3915 3916 if (cxx_record_decl) 3917 { 3918 const RecordDecl *parent_record_decl = cxx_record_decl; 3919 3920 //printf ("parent = %s\n", parent_record_decl->getNameAsCString()); 3921 3922 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl(); 3923 // Didn't find things easily, lets let clang do its thang... 3924 IdentifierInfo & ident_ref = ast->Idents.get(name_sref); 3925 DeclarationName decl_name(&ident_ref); 3926 3927 CXXBasePaths paths; 3928 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember, 3929 decl_name.getAsOpaquePtr(), 3930 paths)) 3931 { 3932 CXXBasePaths::const_paths_iterator path, path_end = paths.end(); 3933 for (path = paths.begin(); path != path_end; ++path) 3934 { 3935 const size_t num_path_elements = path->size(); 3936 for (size_t e=0; e<num_path_elements; ++e) 3937 { 3938 CXXBasePathElement elem = (*path)[e]; 3939 3940 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes); 3941 if (child_idx == UINT32_MAX) 3942 { 3943 child_indexes.clear(); 3944 return 0; 3945 } 3946 else 3947 { 3948 child_indexes.push_back (child_idx); 3949 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl()); 3950 } 3951 } 3952 DeclContext::lookup_iterator named_decl_pos; 3953 for (named_decl_pos = path->Decls.first; 3954 named_decl_pos != path->Decls.second && parent_record_decl; 3955 ++named_decl_pos) 3956 { 3957 //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString()); 3958 3959 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes); 3960 if (child_idx == UINT32_MAX) 3961 { 3962 child_indexes.clear(); 3963 return 0; 3964 } 3965 else 3966 { 3967 child_indexes.push_back (child_idx); 3968 } 3969 } 3970 } 3971 return child_indexes.size(); 3972 } 3973 } 3974 3975 } 3976 break; 3977 3978 case clang::Type::ObjCObject: 3979 case clang::Type::ObjCInterface: 3980 if (GetCompleteQualType (ast, qual_type)) 3981 { 3982 StringRef name_sref(name); 3983 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 3984 assert (objc_class_type); 3985 if (objc_class_type) 3986 { 3987 uint32_t child_idx = 0; 3988 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 3989 3990 if (class_interface_decl) 3991 { 3992 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 3993 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 3994 3995 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx) 3996 { 3997 const ObjCIvarDecl* ivar_decl = *ivar_pos; 3998 3999 if (ivar_decl->getName().equals (name_sref)) 4000 { 4001 if ((!omit_empty_base_classes && superclass_interface_decl) || 4002 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true))) 4003 ++child_idx; 4004 4005 child_indexes.push_back (child_idx); 4006 return child_indexes.size(); 4007 } 4008 } 4009 4010 if (superclass_interface_decl) 4011 { 4012 // The super class index is always zero for ObjC classes, 4013 // so we push it onto the child indexes in case we find 4014 // an ivar in our superclass... 4015 child_indexes.push_back (0); 4016 4017 if (GetIndexOfChildMemberWithName (ast, 4018 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), 4019 name, 4020 omit_empty_base_classes, 4021 child_indexes)) 4022 { 4023 // We did find an ivar in a superclass so just 4024 // return the results! 4025 return child_indexes.size(); 4026 } 4027 4028 // We didn't find an ivar matching "name" in our 4029 // superclass, pop the superclass zero index that 4030 // we pushed on above. 4031 child_indexes.pop_back(); 4032 } 4033 } 4034 } 4035 } 4036 break; 4037 4038 case clang::Type::ObjCObjectPointer: 4039 { 4040 return GetIndexOfChildMemberWithName (ast, 4041 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(), 4042 name, 4043 omit_empty_base_classes, 4044 child_indexes); 4045 } 4046 break; 4047 4048 4049 case clang::Type::ConstantArray: 4050 { 4051// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 4052// const uint64_t element_count = array->getSize().getLimitedValue(); 4053// 4054// if (idx < element_count) 4055// { 4056// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType()); 4057// 4058// char element_name[32]; 4059// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 4060// 4061// child_name.assign(element_name); 4062// assert(field_type_info.first % 8 == 0); 4063// child_byte_size = field_type_info.first / 8; 4064// child_byte_offset = idx * child_byte_size; 4065// return array->getElementType().getAsOpaquePtr(); 4066// } 4067 } 4068 break; 4069 4070// case clang::Type::MemberPointerType: 4071// { 4072// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); 4073// QualType pointee_type = mem_ptr_type->getPointeeType(); 4074// 4075// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 4076// { 4077// return GetIndexOfChildWithName (ast, 4078// mem_ptr_type->getPointeeType().getAsOpaquePtr(), 4079// name); 4080// } 4081// } 4082// break; 4083// 4084 case clang::Type::LValueReference: 4085 case clang::Type::RValueReference: 4086 { 4087 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 4088 QualType pointee_type = reference_type->getPointeeType(); 4089 4090 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 4091 { 4092 return GetIndexOfChildMemberWithName (ast, 4093 reference_type->getPointeeType().getAsOpaquePtr(), 4094 name, 4095 omit_empty_base_classes, 4096 child_indexes); 4097 } 4098 } 4099 break; 4100 4101 case clang::Type::Pointer: 4102 { 4103 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 4104 QualType pointee_type = pointer_type->getPointeeType(); 4105 4106 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 4107 { 4108 return GetIndexOfChildMemberWithName (ast, 4109 pointer_type->getPointeeType().getAsOpaquePtr(), 4110 name, 4111 omit_empty_base_classes, 4112 child_indexes); 4113 } 4114 else 4115 { 4116// if (parent_name) 4117// { 4118// child_name.assign(1, '*'); 4119// child_name += parent_name; 4120// } 4121// 4122// // We have a pointer to an simple type 4123// if (idx == 0) 4124// { 4125// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 4126// assert(clang_type_info.first % 8 == 0); 4127// child_byte_size = clang_type_info.first / 8; 4128// child_byte_offset = 0; 4129// return pointee_type.getAsOpaquePtr(); 4130// } 4131 } 4132 } 4133 break; 4134 4135 case clang::Type::Typedef: 4136 return GetIndexOfChildMemberWithName (ast, 4137 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 4138 name, 4139 omit_empty_base_classes, 4140 child_indexes); 4141 4142 default: 4143 break; 4144 } 4145 } 4146 return 0; 4147} 4148 4149 4150// Get the index of the child of "clang_type" whose name matches. This function 4151// doesn't descend into the children, but only looks one level deep and name 4152// matches can include base class names. 4153 4154uint32_t 4155ClangASTContext::GetIndexOfChildWithName 4156( 4157 ASTContext *ast, 4158 clang_type_t clang_type, 4159 const char *name, 4160 bool omit_empty_base_classes 4161) 4162{ 4163 if (clang_type && name && name[0]) 4164 { 4165 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 4166 4167 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4168 4169 switch (type_class) 4170 { 4171 case clang::Type::Record: 4172 if (GetCompleteQualType (ast, qual_type)) 4173 { 4174 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 4175 const RecordDecl *record_decl = record_type->getDecl(); 4176 4177 assert(record_decl); 4178 uint32_t child_idx = 0; 4179 4180 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 4181 4182 if (cxx_record_decl) 4183 { 4184 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 4185 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 4186 base_class != base_class_end; 4187 ++base_class) 4188 { 4189 // Skip empty base classes 4190 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 4191 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false) 4192 continue; 4193 4194 std::string base_class_type_name (ClangASTType::GetTypeNameForQualType(base_class->getType())); 4195 if (base_class_type_name.compare (name) == 0) 4196 return child_idx; 4197 ++child_idx; 4198 } 4199 } 4200 4201 // Try and find a field that matches NAME 4202 RecordDecl::field_iterator field, field_end; 4203 StringRef name_sref(name); 4204 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 4205 field != field_end; 4206 ++field, ++child_idx) 4207 { 4208 if (field->getName().equals (name_sref)) 4209 return child_idx; 4210 } 4211 4212 } 4213 break; 4214 4215 case clang::Type::ObjCObject: 4216 case clang::Type::ObjCInterface: 4217 if (GetCompleteQualType (ast, qual_type)) 4218 { 4219 StringRef name_sref(name); 4220 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 4221 assert (objc_class_type); 4222 if (objc_class_type) 4223 { 4224 uint32_t child_idx = 0; 4225 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 4226 4227 if (class_interface_decl) 4228 { 4229 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 4230 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 4231 4232 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos) 4233 { 4234 const ObjCIvarDecl* ivar_decl = *ivar_pos; 4235 4236 if (ivar_decl->getName().equals (name_sref)) 4237 { 4238 if ((!omit_empty_base_classes && superclass_interface_decl) || 4239 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true))) 4240 ++child_idx; 4241 4242 return child_idx; 4243 } 4244 } 4245 4246 if (superclass_interface_decl) 4247 { 4248 if (superclass_interface_decl->getName().equals (name_sref)) 4249 return 0; 4250 } 4251 } 4252 } 4253 } 4254 break; 4255 4256 case clang::Type::ObjCObjectPointer: 4257 { 4258 return GetIndexOfChildWithName (ast, 4259 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(), 4260 name, 4261 omit_empty_base_classes); 4262 } 4263 break; 4264 4265 case clang::Type::ConstantArray: 4266 { 4267// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 4268// const uint64_t element_count = array->getSize().getLimitedValue(); 4269// 4270// if (idx < element_count) 4271// { 4272// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType()); 4273// 4274// char element_name[32]; 4275// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 4276// 4277// child_name.assign(element_name); 4278// assert(field_type_info.first % 8 == 0); 4279// child_byte_size = field_type_info.first / 8; 4280// child_byte_offset = idx * child_byte_size; 4281// return array->getElementType().getAsOpaquePtr(); 4282// } 4283 } 4284 break; 4285 4286// case clang::Type::MemberPointerType: 4287// { 4288// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); 4289// QualType pointee_type = mem_ptr_type->getPointeeType(); 4290// 4291// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 4292// { 4293// return GetIndexOfChildWithName (ast, 4294// mem_ptr_type->getPointeeType().getAsOpaquePtr(), 4295// name); 4296// } 4297// } 4298// break; 4299// 4300 case clang::Type::LValueReference: 4301 case clang::Type::RValueReference: 4302 { 4303 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 4304 QualType pointee_type = reference_type->getPointeeType(); 4305 4306 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 4307 { 4308 return GetIndexOfChildWithName (ast, 4309 reference_type->getPointeeType().getAsOpaquePtr(), 4310 name, 4311 omit_empty_base_classes); 4312 } 4313 } 4314 break; 4315 4316 case clang::Type::Pointer: 4317 { 4318 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 4319 QualType pointee_type = pointer_type->getPointeeType(); 4320 4321 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 4322 { 4323 return GetIndexOfChildWithName (ast, 4324 pointer_type->getPointeeType().getAsOpaquePtr(), 4325 name, 4326 omit_empty_base_classes); 4327 } 4328 else 4329 { 4330// if (parent_name) 4331// { 4332// child_name.assign(1, '*'); 4333// child_name += parent_name; 4334// } 4335// 4336// // We have a pointer to an simple type 4337// if (idx == 0) 4338// { 4339// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 4340// assert(clang_type_info.first % 8 == 0); 4341// child_byte_size = clang_type_info.first / 8; 4342// child_byte_offset = 0; 4343// return pointee_type.getAsOpaquePtr(); 4344// } 4345 } 4346 } 4347 break; 4348 4349 case clang::Type::Typedef: 4350 return GetIndexOfChildWithName (ast, 4351 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 4352 name, 4353 omit_empty_base_classes); 4354 4355 default: 4356 break; 4357 } 4358 } 4359 return UINT32_MAX; 4360} 4361 4362#pragma mark TagType 4363 4364bool 4365ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind) 4366{ 4367 if (tag_clang_type) 4368 { 4369 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type)); 4370 const clang::Type *clang_type = tag_qual_type.getTypePtr(); 4371 if (clang_type) 4372 { 4373 const TagType *tag_type = dyn_cast<TagType>(clang_type); 4374 if (tag_type) 4375 { 4376 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl()); 4377 if (tag_decl) 4378 { 4379 tag_decl->setTagKind ((TagDecl::TagKind)kind); 4380 return true; 4381 } 4382 } 4383 } 4384 } 4385 return false; 4386} 4387 4388 4389#pragma mark DeclContext Functions 4390 4391DeclContext * 4392ClangASTContext::GetDeclContextForType (clang_type_t clang_type) 4393{ 4394 if (clang_type == NULL) 4395 return NULL; 4396 4397 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 4398 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4399 switch (type_class) 4400 { 4401 case clang::Type::UnaryTransform: break; 4402 case clang::Type::FunctionNoProto: break; 4403 case clang::Type::FunctionProto: break; 4404 case clang::Type::IncompleteArray: break; 4405 case clang::Type::VariableArray: break; 4406 case clang::Type::ConstantArray: break; 4407 case clang::Type::DependentSizedArray: break; 4408 case clang::Type::ExtVector: break; 4409 case clang::Type::DependentSizedExtVector: break; 4410 case clang::Type::Vector: break; 4411 case clang::Type::Builtin: break; 4412 case clang::Type::BlockPointer: break; 4413 case clang::Type::Pointer: break; 4414 case clang::Type::LValueReference: break; 4415 case clang::Type::RValueReference: break; 4416 case clang::Type::MemberPointer: break; 4417 case clang::Type::Complex: break; 4418 case clang::Type::ObjCObject: break; 4419 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface(); 4420 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr()); 4421 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl(); 4422 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl(); 4423 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 4424 case clang::Type::Elaborated: return ClangASTContext::GetDeclContextForType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 4425 case clang::Type::TypeOfExpr: break; 4426 case clang::Type::TypeOf: break; 4427 case clang::Type::Decltype: break; 4428 //case clang::Type::QualifiedName: break; 4429 case clang::Type::TemplateSpecialization: break; 4430 case clang::Type::DependentTemplateSpecialization: break; 4431 case clang::Type::TemplateTypeParm: break; 4432 case clang::Type::SubstTemplateTypeParm: break; 4433 case clang::Type::SubstTemplateTypeParmPack:break; 4434 case clang::Type::PackExpansion: break; 4435 case clang::Type::UnresolvedUsing: break; 4436 case clang::Type::Paren: break; 4437 case clang::Type::Attributed: break; 4438 case clang::Type::Auto: break; 4439 case clang::Type::InjectedClassName: break; 4440 case clang::Type::DependentName: break; 4441 case clang::Type::Atomic: break; 4442 } 4443 // No DeclContext in this type... 4444 return NULL; 4445} 4446 4447#pragma mark Namespace Declarations 4448 4449NamespaceDecl * 4450ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx) 4451{ 4452 NamespaceDecl *namespace_decl = NULL; 4453 ASTContext *ast = getASTContext(); 4454 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl (); 4455 if (decl_ctx == NULL) 4456 decl_ctx = translation_unit_decl; 4457 4458 if (name) 4459 { 4460 IdentifierInfo &identifier_info = ast->Idents.get(name); 4461 DeclarationName decl_name (&identifier_info); 4462 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); 4463 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos) 4464 { 4465 namespace_decl = dyn_cast<clang::NamespaceDecl>(*pos); 4466 if (namespace_decl) 4467 return namespace_decl; 4468 } 4469 4470 namespace_decl = NamespaceDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), &identifier_info); 4471 4472 decl_ctx->addDecl (namespace_decl); 4473 } 4474 else 4475 { 4476 if (decl_ctx == translation_unit_decl) 4477 { 4478 namespace_decl = translation_unit_decl->getAnonymousNamespace(); 4479 if (namespace_decl) 4480 return namespace_decl; 4481 4482 namespace_decl = NamespaceDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), NULL); 4483 translation_unit_decl->setAnonymousNamespace (namespace_decl); 4484 translation_unit_decl->addDecl (namespace_decl); 4485 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace()); 4486 } 4487 else 4488 { 4489 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx); 4490 if (parent_namespace_decl) 4491 { 4492 namespace_decl = parent_namespace_decl->getAnonymousNamespace(); 4493 if (namespace_decl) 4494 return namespace_decl; 4495 namespace_decl = NamespaceDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), NULL); 4496 parent_namespace_decl->setAnonymousNamespace (namespace_decl); 4497 parent_namespace_decl->addDecl (namespace_decl); 4498 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace()); 4499 } 4500 else 4501 { 4502 // BAD!!! 4503 } 4504 } 4505 4506 4507 if (namespace_decl) 4508 { 4509 // If we make it here, we are creating the anonymous namespace decl 4510 // for the first time, so we need to do the using directive magic 4511 // like SEMA does 4512 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast, 4513 decl_ctx, 4514 SourceLocation(), 4515 SourceLocation(), 4516 NestedNameSpecifierLoc(), 4517 SourceLocation(), 4518 namespace_decl, 4519 decl_ctx); 4520 using_directive_decl->setImplicit(); 4521 decl_ctx->addDecl(using_directive_decl); 4522 } 4523 } 4524#ifdef LLDB_CONFIGURATION_DEBUG 4525 VerifyDecl(namespace_decl); 4526#endif 4527 return namespace_decl; 4528} 4529 4530 4531#pragma mark Function Types 4532 4533FunctionDecl * 4534ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx, const char *name, clang_type_t function_clang_type, int storage, bool is_inline) 4535{ 4536 FunctionDecl *func_decl = NULL; 4537 ASTContext *ast = getASTContext(); 4538 if (decl_ctx == NULL) 4539 decl_ctx = ast->getTranslationUnitDecl(); 4540 4541 if (name && name[0]) 4542 { 4543 func_decl = FunctionDecl::Create (*ast, 4544 decl_ctx, 4545 SourceLocation(), 4546 SourceLocation(), 4547 DeclarationName (&ast->Idents.get(name)), 4548 QualType::getFromOpaquePtr(function_clang_type), 4549 NULL, 4550 (FunctionDecl::StorageClass)storage, 4551 (FunctionDecl::StorageClass)storage, 4552 is_inline); 4553 } 4554 else 4555 { 4556 func_decl = FunctionDecl::Create (*ast, 4557 decl_ctx, 4558 SourceLocation(), 4559 SourceLocation(), 4560 DeclarationName (), 4561 QualType::getFromOpaquePtr(function_clang_type), 4562 NULL, 4563 (FunctionDecl::StorageClass)storage, 4564 (FunctionDecl::StorageClass)storage, 4565 is_inline); 4566 } 4567 if (func_decl) 4568 decl_ctx->addDecl (func_decl); 4569 4570#ifdef LLDB_CONFIGURATION_DEBUG 4571 VerifyDecl(func_decl); 4572#endif 4573 4574 return func_decl; 4575} 4576 4577clang_type_t 4578ClangASTContext::CreateFunctionType (ASTContext *ast, 4579 clang_type_t result_type, 4580 clang_type_t *args, 4581 unsigned num_args, 4582 bool is_variadic, 4583 unsigned type_quals) 4584{ 4585 assert (ast != NULL); 4586 std::vector<QualType> qual_type_args; 4587 for (unsigned i=0; i<num_args; ++i) 4588 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i])); 4589 4590 // TODO: Detect calling convention in DWARF? 4591 FunctionProtoType::ExtProtoInfo proto_info; 4592 proto_info.Variadic = is_variadic; 4593 proto_info.ExceptionSpecType = EST_None; 4594 proto_info.TypeQuals = type_quals; 4595 proto_info.RefQualifier = RQ_None; 4596 proto_info.NumExceptions = 0; 4597 proto_info.Exceptions = NULL; 4598 4599 return ast->getFunctionType (QualType::getFromOpaquePtr(result_type), 4600 qual_type_args.empty() ? NULL : &qual_type_args.front(), 4601 qual_type_args.size(), 4602 proto_info).getAsOpaquePtr(); // NoReturn); 4603} 4604 4605ParmVarDecl * 4606ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage) 4607{ 4608 ASTContext *ast = getASTContext(); 4609 assert (ast != NULL); 4610 return ParmVarDecl::Create(*ast, 4611 ast->getTranslationUnitDecl(), 4612 SourceLocation(), 4613 SourceLocation(), 4614 name && name[0] ? &ast->Idents.get(name) : NULL, 4615 QualType::getFromOpaquePtr(param_type), 4616 NULL, 4617 (VarDecl::StorageClass)storage, 4618 (VarDecl::StorageClass)storage, 4619 0); 4620} 4621 4622void 4623ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params) 4624{ 4625 if (function_decl) 4626 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params)); 4627} 4628 4629 4630#pragma mark Array Types 4631 4632clang_type_t 4633ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count, uint32_t bit_stride) 4634{ 4635 if (element_type) 4636 { 4637 ASTContext *ast = getASTContext(); 4638 assert (ast != NULL); 4639 llvm::APInt ap_element_count (64, element_count); 4640 return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type), 4641 ap_element_count, 4642 ArrayType::Normal, 4643 0).getAsOpaquePtr(); // ElemQuals 4644 } 4645 return NULL; 4646} 4647 4648 4649#pragma mark TagDecl 4650 4651bool 4652ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type) 4653{ 4654 if (clang_type) 4655 { 4656 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4657 const clang::Type *t = qual_type.getTypePtr(); 4658 if (t) 4659 { 4660 const TagType *tag_type = dyn_cast<TagType>(t); 4661 if (tag_type) 4662 { 4663 TagDecl *tag_decl = tag_type->getDecl(); 4664 if (tag_decl) 4665 { 4666 tag_decl->startDefinition(); 4667 return true; 4668 } 4669 } 4670 } 4671 } 4672 return false; 4673} 4674 4675bool 4676ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type) 4677{ 4678 if (clang_type) 4679 { 4680 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4681 4682 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 4683 4684 if (cxx_record_decl) 4685 { 4686 cxx_record_decl->completeDefinition(); 4687 4688 return true; 4689 } 4690 4691 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type); 4692 4693 if (objc_class_type) 4694 { 4695 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 4696 4697 class_interface_decl->completedForwardDecl(); 4698 } 4699 4700 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr()); 4701 4702 if (enum_type) 4703 { 4704 EnumDecl *enum_decl = enum_type->getDecl(); 4705 4706 if (enum_decl) 4707 { 4708 /// TODO This really needs to be fixed. 4709 4710 unsigned NumPositiveBits = 1; 4711 unsigned NumNegativeBits = 0; 4712 4713 ASTContext *ast = getASTContext(); 4714 4715 QualType promotion_qual_type; 4716 // If the enum integer type is less than an integer in bit width, 4717 // then we must promote it to an integer size. 4718 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy)) 4719 { 4720 if (enum_decl->getIntegerType()->isSignedIntegerType()) 4721 promotion_qual_type = ast->IntTy; 4722 else 4723 promotion_qual_type = ast->UnsignedIntTy; 4724 } 4725 else 4726 promotion_qual_type = enum_decl->getIntegerType(); 4727 4728 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits); 4729 return true; 4730 } 4731 } 4732 } 4733 return false; 4734} 4735 4736 4737#pragma mark Enumeration Types 4738 4739clang_type_t 4740ClangASTContext::CreateEnumerationType 4741( 4742 const char *name, 4743 DeclContext *decl_ctx, 4744 const Declaration &decl, 4745 clang_type_t integer_qual_type 4746) 4747{ 4748 // TODO: Do something intelligent with the Declaration object passed in 4749 // like maybe filling in the SourceLocation with it... 4750 ASTContext *ast = getASTContext(); 4751 assert (ast != NULL); 4752 4753 // TODO: ask about these... 4754// const bool IsScoped = false; 4755// const bool IsFixed = false; 4756 4757 EnumDecl *enum_decl = EnumDecl::Create (*ast, 4758 decl_ctx, 4759 SourceLocation(), 4760 SourceLocation(), 4761 name && name[0] ? &ast->Idents.get(name) : NULL, 4762 NULL, 4763 false, // IsScoped 4764 false, // IsScopedUsingClassTag 4765 false); // IsFixed 4766 4767 4768 if (enum_decl) 4769 { 4770 // TODO: check if we should be setting the promotion type too? 4771 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type)); 4772 4773 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info 4774 4775 return ast->getTagDeclType(enum_decl).getAsOpaquePtr(); 4776 } 4777 return NULL; 4778} 4779 4780clang_type_t 4781ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type) 4782{ 4783 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type)); 4784 4785 const clang::Type *clang_type = enum_qual_type.getTypePtr(); 4786 if (clang_type) 4787 { 4788 const EnumType *enum_type = dyn_cast<EnumType>(clang_type); 4789 if (enum_type) 4790 { 4791 EnumDecl *enum_decl = enum_type->getDecl(); 4792 if (enum_decl) 4793 return enum_decl->getIntegerType().getAsOpaquePtr(); 4794 } 4795 } 4796 return NULL; 4797} 4798bool 4799ClangASTContext::AddEnumerationValueToEnumerationType 4800( 4801 clang_type_t enum_clang_type, 4802 clang_type_t enumerator_clang_type, 4803 const Declaration &decl, 4804 const char *name, 4805 int64_t enum_value, 4806 uint32_t enum_value_bit_size 4807) 4808{ 4809 if (enum_clang_type && enumerator_clang_type && name) 4810 { 4811 // TODO: Do something intelligent with the Declaration object passed in 4812 // like maybe filling in the SourceLocation with it... 4813 ASTContext *ast = getASTContext(); 4814 IdentifierTable *identifier_table = getIdentifierTable(); 4815 4816 assert (ast != NULL); 4817 assert (identifier_table != NULL); 4818 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type)); 4819 4820 const clang::Type *clang_type = enum_qual_type.getTypePtr(); 4821 if (clang_type) 4822 { 4823 const EnumType *enum_type = dyn_cast<EnumType>(clang_type); 4824 4825 if (enum_type) 4826 { 4827 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false); 4828 enum_llvm_apsint = enum_value; 4829 EnumConstantDecl *enumerator_decl = 4830 EnumConstantDecl::Create (*ast, 4831 enum_type->getDecl(), 4832 SourceLocation(), 4833 name ? &identifier_table->get(name) : NULL, // Identifier 4834 QualType::getFromOpaquePtr(enumerator_clang_type), 4835 NULL, 4836 enum_llvm_apsint); 4837 4838 if (enumerator_decl) 4839 { 4840 enum_type->getDecl()->addDecl(enumerator_decl); 4841 4842#ifdef LLDB_CONFIGURATION_DEBUG 4843 VerifyDecl(enumerator_decl); 4844#endif 4845 4846 return true; 4847 } 4848 } 4849 } 4850 } 4851 return false; 4852} 4853 4854#pragma mark Pointers & References 4855 4856clang_type_t 4857ClangASTContext::CreatePointerType (clang_type_t clang_type) 4858{ 4859 return CreatePointerType (getASTContext(), clang_type); 4860} 4861 4862clang_type_t 4863ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type) 4864{ 4865 if (ast && clang_type) 4866 { 4867 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4868 4869 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4870 switch (type_class) 4871 { 4872 case clang::Type::ObjCObject: 4873 case clang::Type::ObjCInterface: 4874 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr(); 4875 4876 default: 4877 return ast->getPointerType(qual_type).getAsOpaquePtr(); 4878 } 4879 } 4880 return NULL; 4881} 4882 4883clang_type_t 4884ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast, 4885 clang_type_t clang_type) 4886{ 4887 if (clang_type) 4888 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 4889 return NULL; 4890} 4891 4892clang_type_t 4893ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast, 4894 clang_type_t clang_type) 4895{ 4896 if (clang_type) 4897 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 4898 return NULL; 4899} 4900 4901clang_type_t 4902ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type) 4903{ 4904 if (clang_pointee_type && clang_pointee_type) 4905 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type), 4906 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr(); 4907 return NULL; 4908} 4909 4910uint32_t 4911ClangASTContext::GetPointerBitSize () 4912{ 4913 ASTContext *ast = getASTContext(); 4914 return ast->getTypeSize(ast->VoidPtrTy); 4915} 4916 4917bool 4918ClangASTContext::IsPossibleDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type) 4919{ 4920 QualType pointee_qual_type; 4921 if (clang_type) 4922 { 4923 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4924 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4925 bool success = false; 4926 switch (type_class) 4927 { 4928 case clang::Type::Builtin: 4929 if (cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId) 4930 { 4931 if (dynamic_pointee_type) 4932 *dynamic_pointee_type = clang_type; 4933 return true; 4934 } 4935 break; 4936 4937 case clang::Type::ObjCObjectPointer: 4938 if (dynamic_pointee_type) 4939 *dynamic_pointee_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 4940 return true; 4941 4942 case clang::Type::Pointer: 4943 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType(); 4944 success = true; 4945 break; 4946 4947 case clang::Type::LValueReference: 4948 case clang::Type::RValueReference: 4949 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType(); 4950 success = true; 4951 break; 4952 4953 case clang::Type::Typedef: 4954 return ClangASTContext::IsPossibleDynamicType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), dynamic_pointee_type); 4955 4956 case clang::Type::Elaborated: 4957 return ClangASTContext::IsPossibleDynamicType (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), dynamic_pointee_type); 4958 4959 default: 4960 break; 4961 } 4962 4963 if (success) 4964 { 4965 // Check to make sure what we are pointing too is a possible dynamic C++ type 4966 // We currently accept any "void *" (in case we have a class that has been 4967 // watered down to an opaque pointer) and virtual C++ classes. 4968 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass(); 4969 switch (pointee_type_class) 4970 { 4971 case clang::Type::Builtin: 4972 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind()) 4973 { 4974 case clang::BuiltinType::UnknownAny: 4975 case clang::BuiltinType::Void: 4976 if (dynamic_pointee_type) 4977 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr(); 4978 return true; 4979 4980 case clang::BuiltinType::NullPtr: 4981 case clang::BuiltinType::Bool: 4982 case clang::BuiltinType::Char_U: 4983 case clang::BuiltinType::UChar: 4984 case clang::BuiltinType::WChar_U: 4985 case clang::BuiltinType::Char16: 4986 case clang::BuiltinType::Char32: 4987 case clang::BuiltinType::UShort: 4988 case clang::BuiltinType::UInt: 4989 case clang::BuiltinType::ULong: 4990 case clang::BuiltinType::ULongLong: 4991 case clang::BuiltinType::UInt128: 4992 case clang::BuiltinType::Char_S: 4993 case clang::BuiltinType::SChar: 4994 case clang::BuiltinType::WChar_S: 4995 case clang::BuiltinType::Short: 4996 case clang::BuiltinType::Int: 4997 case clang::BuiltinType::Long: 4998 case clang::BuiltinType::LongLong: 4999 case clang::BuiltinType::Int128: 5000 case clang::BuiltinType::Float: 5001 case clang::BuiltinType::Double: 5002 case clang::BuiltinType::LongDouble: 5003 case clang::BuiltinType::Dependent: 5004 case clang::BuiltinType::Overload: 5005 case clang::BuiltinType::ObjCId: 5006 case clang::BuiltinType::ObjCClass: 5007 case clang::BuiltinType::ObjCSel: 5008 case clang::BuiltinType::BoundMember: 5009 case clang::BuiltinType::Half: 5010 case clang::BuiltinType::ARCUnbridgedCast: 5011 case clang::BuiltinType::PseudoObject: 5012 break; 5013 } 5014 break; 5015 5016 case clang::Type::Record: 5017 { 5018 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl(); 5019 if (cxx_record_decl) 5020 { 5021 if (GetCompleteQualType (ast, pointee_qual_type)) 5022 { 5023 success = cxx_record_decl->isDynamicClass(); 5024 } 5025 else 5026 { 5027 // We failed to get the complete type, so we have to 5028 // treat this as a void * which we might possibly be 5029 // able to complete 5030 success = true; 5031 } 5032 if (success) 5033 { 5034 if (dynamic_pointee_type) 5035 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr(); 5036 return true; 5037 } 5038 } 5039 } 5040 break; 5041 5042 case clang::Type::ObjCObject: 5043 case clang::Type::ObjCInterface: 5044 { 5045 const clang::ObjCObjectType *objc_class_type = pointee_qual_type->getAsObjCQualifiedInterfaceType(); 5046 if (objc_class_type) 5047 { 5048 GetCompleteQualType (ast, pointee_qual_type); 5049 if (dynamic_pointee_type) 5050 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr(); 5051 return true; 5052 } 5053 } 5054 break; 5055 5056 default: 5057 break; 5058 } 5059 } 5060 } 5061 if (dynamic_pointee_type) 5062 *dynamic_pointee_type = NULL; 5063 return false; 5064} 5065 5066 5067bool 5068ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type) 5069{ 5070 QualType pointee_qual_type; 5071 if (clang_type) 5072 { 5073 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5074 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5075 bool success = false; 5076 switch (type_class) 5077 { 5078 case clang::Type::Pointer: 5079 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType(); 5080 success = true; 5081 break; 5082 5083 case clang::Type::LValueReference: 5084 case clang::Type::RValueReference: 5085 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType(); 5086 success = true; 5087 break; 5088 5089 case clang::Type::Typedef: 5090 return ClangASTContext::IsPossibleCPlusPlusDynamicType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), dynamic_pointee_type); 5091 5092 case clang::Type::Elaborated: 5093 return ClangASTContext::IsPossibleCPlusPlusDynamicType (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 5094 5095 default: 5096 break; 5097 } 5098 5099 if (success) 5100 { 5101 // Check to make sure what we are pointing too is a possible dynamic C++ type 5102 // We currently accept any "void *" (in case we have a class that has been 5103 // watered down to an opaque pointer) and virtual C++ classes. 5104 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass(); 5105 switch (pointee_type_class) 5106 { 5107 case clang::Type::Builtin: 5108 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind()) 5109 { 5110 case clang::BuiltinType::UnknownAny: 5111 case clang::BuiltinType::Void: 5112 if (dynamic_pointee_type) 5113 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr(); 5114 return true; 5115 5116 case clang::BuiltinType::NullPtr: 5117 case clang::BuiltinType::Bool: 5118 case clang::BuiltinType::Char_U: 5119 case clang::BuiltinType::UChar: 5120 case clang::BuiltinType::WChar_U: 5121 case clang::BuiltinType::Char16: 5122 case clang::BuiltinType::Char32: 5123 case clang::BuiltinType::UShort: 5124 case clang::BuiltinType::UInt: 5125 case clang::BuiltinType::ULong: 5126 case clang::BuiltinType::ULongLong: 5127 case clang::BuiltinType::UInt128: 5128 case clang::BuiltinType::Char_S: 5129 case clang::BuiltinType::SChar: 5130 case clang::BuiltinType::WChar_S: 5131 case clang::BuiltinType::Short: 5132 case clang::BuiltinType::Int: 5133 case clang::BuiltinType::Long: 5134 case clang::BuiltinType::LongLong: 5135 case clang::BuiltinType::Int128: 5136 case clang::BuiltinType::Float: 5137 case clang::BuiltinType::Double: 5138 case clang::BuiltinType::LongDouble: 5139 case clang::BuiltinType::Dependent: 5140 case clang::BuiltinType::Overload: 5141 case clang::BuiltinType::ObjCId: 5142 case clang::BuiltinType::ObjCClass: 5143 case clang::BuiltinType::ObjCSel: 5144 case clang::BuiltinType::BoundMember: 5145 case clang::BuiltinType::Half: 5146 case clang::BuiltinType::ARCUnbridgedCast: 5147 case clang::BuiltinType::PseudoObject: 5148 break; 5149 } 5150 break; 5151 case clang::Type::Record: 5152 { 5153 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl(); 5154 if (cxx_record_decl) 5155 { 5156 if (GetCompleteQualType (ast, pointee_qual_type)) 5157 { 5158 success = cxx_record_decl->isDynamicClass(); 5159 } 5160 else 5161 { 5162 // We failed to get the complete type, so we have to 5163 // treat this as a void * which we might possibly be 5164 // able to complete 5165 success = true; 5166 } 5167 if (success) 5168 { 5169 if (dynamic_pointee_type) 5170 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr(); 5171 return true; 5172 } 5173 } 5174 } 5175 break; 5176 5177 default: 5178 break; 5179 } 5180 } 5181 } 5182 if (dynamic_pointee_type) 5183 *dynamic_pointee_type = NULL; 5184 return false; 5185} 5186 5187bool 5188ClangASTContext::IsReferenceType (clang_type_t clang_type, clang_type_t *target_type) 5189{ 5190 if (clang_type == NULL) 5191 return false; 5192 5193 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5194 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5195 5196 switch (type_class) 5197 { 5198 case clang::Type::LValueReference: 5199 if (target_type) 5200 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 5201 return true; 5202 case clang::Type::RValueReference: 5203 if (target_type) 5204 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 5205 return true; 5206 case clang::Type::Typedef: 5207 return ClangASTContext::IsReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 5208 case clang::Type::Elaborated: 5209 return ClangASTContext::IsReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 5210 default: 5211 break; 5212 } 5213 5214 return false; 5215} 5216 5217bool 5218ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type) 5219{ 5220 if (clang_type == NULL) 5221 return false; 5222 5223 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5224 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5225 switch (type_class) 5226 { 5227 case clang::Type::Builtin: 5228 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 5229 { 5230 default: 5231 break; 5232 case clang::BuiltinType::ObjCId: 5233 case clang::BuiltinType::ObjCClass: 5234 return true; 5235 } 5236 return false; 5237 case clang::Type::ObjCObjectPointer: 5238 if (target_type) 5239 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5240 return true; 5241 case clang::Type::BlockPointer: 5242 if (target_type) 5243 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5244 return true; 5245 case clang::Type::Pointer: 5246 if (target_type) 5247 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5248 return true; 5249 case clang::Type::MemberPointer: 5250 if (target_type) 5251 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5252 return true; 5253 case clang::Type::LValueReference: 5254 if (target_type) 5255 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 5256 return true; 5257 case clang::Type::RValueReference: 5258 if (target_type) 5259 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 5260 return true; 5261 case clang::Type::Typedef: 5262 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 5263 case clang::Type::Elaborated: 5264 return ClangASTContext::IsPointerOrReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 5265 default: 5266 break; 5267 } 5268 return false; 5269} 5270 5271bool 5272ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed) 5273{ 5274 if (!clang_type) 5275 return false; 5276 5277 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5278 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()); 5279 5280 if (builtin_type) 5281 { 5282 if (builtin_type->isInteger()) 5283 is_signed = builtin_type->isSignedInteger(); 5284 5285 return true; 5286 } 5287 5288 return false; 5289} 5290 5291bool 5292ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t *target_type) 5293{ 5294 if (target_type) 5295 *target_type = NULL; 5296 5297 if (clang_type) 5298 { 5299 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5300 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5301 switch (type_class) 5302 { 5303 case clang::Type::Builtin: 5304 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 5305 { 5306 default: 5307 break; 5308 case clang::BuiltinType::ObjCId: 5309 case clang::BuiltinType::ObjCClass: 5310 return true; 5311 } 5312 return false; 5313 case clang::Type::ObjCObjectPointer: 5314 if (target_type) 5315 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5316 return true; 5317 case clang::Type::BlockPointer: 5318 if (target_type) 5319 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5320 return true; 5321 case clang::Type::Pointer: 5322 if (target_type) 5323 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5324 return true; 5325 case clang::Type::MemberPointer: 5326 if (target_type) 5327 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 5328 return true; 5329 case clang::Type::Typedef: 5330 return ClangASTContext::IsPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type); 5331 case clang::Type::Elaborated: 5332 return ClangASTContext::IsPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), target_type); 5333 default: 5334 break; 5335 } 5336 } 5337 return false; 5338} 5339 5340bool 5341ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex) 5342{ 5343 if (clang_type) 5344 { 5345 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5346 5347 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal())) 5348 { 5349 clang::BuiltinType::Kind kind = BT->getKind(); 5350 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble) 5351 { 5352 count = 1; 5353 is_complex = false; 5354 return true; 5355 } 5356 } 5357 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal())) 5358 { 5359 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex)) 5360 { 5361 count = 2; 5362 is_complex = true; 5363 return true; 5364 } 5365 } 5366 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal())) 5367 { 5368 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex)) 5369 { 5370 count = VT->getNumElements(); 5371 is_complex = false; 5372 return true; 5373 } 5374 } 5375 } 5376 return false; 5377} 5378 5379bool 5380ClangASTContext::IsScalarType (lldb::clang_type_t clang_type) 5381{ 5382 bool is_signed; 5383 if (ClangASTContext::IsIntegerType(clang_type, is_signed)) 5384 return true; 5385 5386 uint32_t count; 5387 bool is_complex; 5388 return ClangASTContext::IsFloatingPointType(clang_type, count, is_complex) && !is_complex; 5389} 5390 5391bool 5392ClangASTContext::IsPointerToScalarType (lldb::clang_type_t clang_type) 5393{ 5394 if (!IsPointerType(clang_type)) 5395 return false; 5396 5397 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5398 lldb::clang_type_t pointee_type = qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr(); 5399 return IsScalarType(pointee_type); 5400} 5401 5402bool 5403ClangASTContext::IsArrayOfScalarType (lldb::clang_type_t clang_type) 5404{ 5405 if (!IsArrayType(clang_type)) 5406 return false; 5407 5408 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5409 lldb::clang_type_t item_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr(); 5410 return IsScalarType(item_type); 5411} 5412 5413 5414bool 5415ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name) 5416{ 5417 if (clang_type) 5418 { 5419 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5420 5421 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 5422 if (cxx_record_decl) 5423 { 5424 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart()); 5425 return true; 5426 } 5427 } 5428 class_name.clear(); 5429 return false; 5430} 5431 5432 5433bool 5434ClangASTContext::IsCXXClassType (clang_type_t clang_type) 5435{ 5436 if (clang_type) 5437 { 5438 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5439 if (qual_type->getAsCXXRecordDecl() != NULL) 5440 return true; 5441 } 5442 return false; 5443} 5444 5445bool 5446ClangASTContext::IsBeingDefined (lldb::clang_type_t clang_type) 5447{ 5448 if (clang_type) 5449 { 5450 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5451 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type); 5452 if (tag_type) 5453 return tag_type->isBeingDefined(); 5454 } 5455 return false; 5456} 5457 5458bool 5459ClangASTContext::IsObjCClassType (clang_type_t clang_type) 5460{ 5461 if (clang_type) 5462 { 5463 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5464 if (qual_type->isObjCObjectOrInterfaceType()) 5465 return true; 5466 } 5467 return false; 5468} 5469 5470 5471bool 5472ClangASTContext::IsCharType (clang_type_t clang_type) 5473{ 5474 if (clang_type) 5475 return QualType::getFromOpaquePtr(clang_type)->isCharType(); 5476 return false; 5477} 5478 5479bool 5480ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length) 5481{ 5482 clang_type_t pointee_or_element_clang_type = NULL; 5483 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type)); 5484 5485 if (pointee_or_element_clang_type == NULL) 5486 return false; 5487 5488 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer)) 5489 { 5490 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type)); 5491 5492 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType()) 5493 { 5494 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5495 if (type_flags.Test (eTypeIsArray)) 5496 { 5497 // We know the size of the array and it could be a C string 5498 // since it is an array of characters 5499 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue(); 5500 return true; 5501 } 5502 else 5503 { 5504 length = 0; 5505 return true; 5506 } 5507 5508 } 5509 } 5510 return false; 5511} 5512 5513bool 5514ClangASTContext::IsFunctionPointerType (clang_type_t clang_type) 5515{ 5516 if (clang_type) 5517 { 5518 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5519 5520 if (qual_type->isFunctionPointerType()) 5521 return true; 5522 5523 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5524 switch (type_class) 5525 { 5526 default: 5527 break; 5528 case clang::Type::Typedef: 5529 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 5530 case clang::Type::Elaborated: 5531 return ClangASTContext::IsFunctionPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 5532 5533 case clang::Type::LValueReference: 5534 case clang::Type::RValueReference: 5535 { 5536 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 5537 if (reference_type) 5538 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr()); 5539 } 5540 break; 5541 } 5542 } 5543 return false; 5544} 5545 5546size_t 5547ClangASTContext::GetArraySize (clang_type_t clang_type) 5548{ 5549 if (clang_type) 5550 { 5551 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 5552 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5553 switch (type_class) 5554 { 5555 case clang::Type::ConstantArray: 5556 { 5557 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr()); 5558 if (array) 5559 return array->getSize().getLimitedValue(); 5560 } 5561 break; 5562 5563 case clang::Type::Typedef: 5564 return ClangASTContext::GetArraySize(cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 5565 5566 case clang::Type::Elaborated: 5567 return ClangASTContext::GetArraySize(cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()); 5568 5569 default: 5570 break; 5571 } 5572 } 5573 return 0; 5574} 5575 5576bool 5577ClangASTContext::IsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size) 5578{ 5579 if (!clang_type) 5580 return false; 5581 5582 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5583 5584 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5585 switch (type_class) 5586 { 5587 default: 5588 break; 5589 5590 case clang::Type::ConstantArray: 5591 if (member_type) 5592 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 5593 if (size) 5594 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX); 5595 return true; 5596 5597 case clang::Type::IncompleteArray: 5598 if (member_type) 5599 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 5600 if (size) 5601 *size = 0; 5602 return true; 5603 5604 case clang::Type::VariableArray: 5605 if (member_type) 5606 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 5607 if (size) 5608 *size = 0; 5609 return true; 5610 5611 case clang::Type::DependentSizedArray: 5612 if (member_type) 5613 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 5614 if (size) 5615 *size = 0; 5616 return true; 5617 5618 case clang::Type::Typedef: 5619 return ClangASTContext::IsArrayType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 5620 member_type, 5621 size); 5622 5623 case clang::Type::Elaborated: 5624 return ClangASTContext::IsArrayType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), 5625 member_type, 5626 size); 5627 } 5628 return false; 5629} 5630 5631 5632#pragma mark Typedefs 5633 5634clang_type_t 5635ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx) 5636{ 5637 if (clang_type) 5638 { 5639 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5640 ASTContext *ast = getASTContext(); 5641 IdentifierTable *identifier_table = getIdentifierTable(); 5642 assert (ast != NULL); 5643 assert (identifier_table != NULL); 5644 if (decl_ctx == NULL) 5645 decl_ctx = ast->getTranslationUnitDecl(); 5646 TypedefDecl *decl = TypedefDecl::Create (*ast, 5647 decl_ctx, 5648 SourceLocation(), 5649 SourceLocation(), 5650 name ? &identifier_table->get(name) : NULL, // Identifier 5651 ast->CreateTypeSourceInfo(qual_type)); 5652 5653 //decl_ctx->addDecl (decl); 5654 5655 decl->setAccess(AS_public); // TODO respect proper access specifier 5656 5657 // Get a uniqued QualType for the typedef decl type 5658 return ast->getTypedefType (decl).getAsOpaquePtr(); 5659 } 5660 return NULL; 5661} 5662 5663// Disable this for now since I can't seem to get a nicely formatted float 5664// out of the APFloat class without just getting the float, double or quad 5665// and then using a formatted print on it which defeats the purpose. We ideally 5666// would like to get perfect string values for any kind of float semantics 5667// so we can support remote targets. The code below also requires a patch to 5668// llvm::APInt. 5669//bool 5670//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) 5671//{ 5672// uint32_t count = 0; 5673// bool is_complex = false; 5674// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 5675// { 5676// unsigned num_bytes_per_float = byte_size / count; 5677// unsigned num_bits_per_float = num_bytes_per_float * 8; 5678// 5679// float_str.clear(); 5680// uint32_t i; 5681// for (i=0; i<count; i++) 5682// { 5683// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order); 5684// bool is_ieee = false; 5685// APFloat ap_float(ap_int, is_ieee); 5686// char s[1024]; 5687// unsigned int hex_digits = 0; 5688// bool upper_case = false; 5689// 5690// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0) 5691// { 5692// if (i > 0) 5693// float_str.append(", "); 5694// float_str.append(s); 5695// if (i == 1 && is_complex) 5696// float_str.append(1, 'i'); 5697// } 5698// } 5699// return !float_str.empty(); 5700// } 5701// return false; 5702//} 5703 5704size_t 5705ClangASTContext::ConvertStringToFloatValue (ASTContext *ast, clang_type_t clang_type, const char *s, uint8_t *dst, size_t dst_size) 5706{ 5707 if (clang_type) 5708 { 5709 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5710 uint32_t count = 0; 5711 bool is_complex = false; 5712 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 5713 { 5714 // TODO: handle complex and vector types 5715 if (count != 1) 5716 return false; 5717 5718 StringRef s_sref(s); 5719 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref); 5720 5721 const uint64_t bit_size = ast->getTypeSize (qual_type); 5722 const uint64_t byte_size = bit_size / 8; 5723 if (dst_size >= byte_size) 5724 { 5725 if (bit_size == sizeof(float)*8) 5726 { 5727 float float32 = ap_float.convertToFloat(); 5728 ::memcpy (dst, &float32, byte_size); 5729 return byte_size; 5730 } 5731 else if (bit_size >= 64) 5732 { 5733 llvm::APInt ap_int(ap_float.bitcastToAPInt()); 5734 ::memcpy (dst, ap_int.getRawData(), byte_size); 5735 return byte_size; 5736 } 5737 } 5738 } 5739 } 5740 return 0; 5741} 5742 5743unsigned 5744ClangASTContext::GetTypeQualifiers(clang_type_t clang_type) 5745{ 5746 assert (clang_type); 5747 5748 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 5749 5750 return qual_type.getQualifiers().getCVRQualifiers(); 5751} 5752 5753bool 5754ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type) 5755{ 5756 if (clang_type == NULL) 5757 return false; 5758 5759 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type)); 5760} 5761 5762 5763bool 5764ClangASTContext::GetCompleteType (clang_type_t clang_type) 5765{ 5766 return ClangASTContext::GetCompleteType (getASTContext(), clang_type); 5767} 5768 5769bool 5770ClangASTContext::GetCompleteDecl (clang::ASTContext *ast, 5771 clang::Decl *decl) 5772{ 5773 if (!decl) 5774 return false; 5775 5776 ExternalASTSource *ast_source = ast->getExternalSource(); 5777 5778 if (!ast_source) 5779 return false; 5780 5781 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) 5782 { 5783 if (tag_decl->getDefinition()) 5784 return true; 5785 5786 if (!tag_decl->hasExternalLexicalStorage()) 5787 return false; 5788 5789 ast_source->CompleteType(tag_decl); 5790 5791 return !tag_decl->getTypeForDecl()->isIncompleteType(); 5792 } 5793 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) 5794 { 5795 if (!objc_interface_decl->isForwardDecl()) 5796 return true; 5797 5798 if (!objc_interface_decl->hasExternalLexicalStorage()) 5799 return false; 5800 5801 ast_source->CompleteType(objc_interface_decl); 5802 5803 return !objc_interface_decl->isForwardDecl(); 5804 } 5805 else 5806 { 5807 return false; 5808 } 5809} 5810 5811clang::DeclContext * 5812ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl) 5813{ 5814 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl); 5815} 5816 5817clang::DeclContext * 5818ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl) 5819{ 5820 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl); 5821} 5822 5823