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