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