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