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