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