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