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