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