ClangASTContext.cpp revision 344779
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#include "llvm/Support/FormatAdapters.h" 13#include "llvm/Support/FormatVariadic.h" 14 15#include <mutex> 16#include <string> 17#include <vector> 18 19 20// Clang headers like to use NDEBUG inside of them to enable/disable debug 21// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing 22// or another. This is bad because it means that if clang was built in release 23// mode, it assumes that you are building in release mode which is not always 24// the case. You can end up with functions that are defined as empty in header 25// files when NDEBUG is not defined, and this can cause link errors with the 26// clang .a files that you have since you might be missing functions in the .a 27// file. So we have to define NDEBUG when including clang headers to avoid any 28// mismatches. This is covered by rdar://problem/8691220 29 30#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF) 31#define LLDB_DEFINED_NDEBUG_FOR_CLANG 32#define NDEBUG 33// Need to include assert.h so it is as clang would expect it to be (disabled) 34#include <assert.h> 35#endif 36 37#include "clang/AST/ASTContext.h" 38#include "clang/AST/ASTImporter.h" 39#include "clang/AST/Attr.h" 40#include "clang/AST/CXXInheritance.h" 41#include "clang/AST/DeclObjC.h" 42#include "clang/AST/DeclTemplate.h" 43#include "clang/AST/Mangle.h" 44#include "clang/AST/RecordLayout.h" 45#include "clang/AST/Type.h" 46#include "clang/AST/VTableBuilder.h" 47#include "clang/Basic/Builtins.h" 48#include "clang/Basic/Diagnostic.h" 49#include "clang/Basic/FileManager.h" 50#include "clang/Basic/FileSystemOptions.h" 51#include "clang/Basic/SourceManager.h" 52#include "clang/Basic/TargetInfo.h" 53#include "clang/Basic/TargetOptions.h" 54#include "clang/Frontend/FrontendOptions.h" 55#include "clang/Frontend/LangStandard.h" 56 57#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG 58#undef NDEBUG 59#undef LLDB_DEFINED_NDEBUG_FOR_CLANG 60// Need to re-include assert.h so it is as _we_ would expect it to be (enabled) 61#include <assert.h> 62#endif 63 64#include "llvm/Support/Signals.h" 65#include "llvm/Support/Threading.h" 66 67#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h" 68#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h" 69#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h" 70#include "lldb/Utility/ArchSpec.h" 71#include "lldb/Utility/Flags.h" 72 73#include "lldb/Core/DumpDataExtractor.h" 74#include "lldb/Core/Module.h" 75#include "lldb/Core/PluginManager.h" 76#include "lldb/Core/StreamFile.h" 77#include "lldb/Core/ThreadSafeDenseMap.h" 78#include "lldb/Core/UniqueCStringMap.h" 79#include "lldb/Symbol/ClangASTContext.h" 80#include "lldb/Symbol/ClangASTImporter.h" 81#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h" 82#include "lldb/Symbol/ClangExternalASTSourceCommon.h" 83#include "lldb/Symbol/ClangUtil.h" 84#include "lldb/Symbol/ObjectFile.h" 85#include "lldb/Symbol/SymbolFile.h" 86#include "lldb/Symbol/VerifyDecl.h" 87#include "lldb/Target/ExecutionContext.h" 88#include "lldb/Target/Language.h" 89#include "lldb/Target/ObjCLanguageRuntime.h" 90#include "lldb/Target/Process.h" 91#include "lldb/Target/Target.h" 92#include "lldb/Utility/DataExtractor.h" 93#include "lldb/Utility/LLDBAssert.h" 94#include "lldb/Utility/Log.h" 95#include "lldb/Utility/RegularExpression.h" 96#include "lldb/Utility/Scalar.h" 97 98#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h" 99#ifdef LLDB_ENABLE_ALL 100#include "Plugins/SymbolFile/PDB/PDBASTParser.h" 101#endif // LLDB_ENABLE_ALL 102 103#include <stdio.h> 104 105#include <mutex> 106 107using namespace lldb; 108using namespace lldb_private; 109using namespace llvm; 110using namespace clang; 111 112namespace { 113static inline bool 114ClangASTContextSupportsLanguage(lldb::LanguageType language) { 115 return language == eLanguageTypeUnknown || // Clang is the default type system 116 Language::LanguageIsC(language) || 117 Language::LanguageIsCPlusPlus(language) || 118 Language::LanguageIsObjC(language) || 119 Language::LanguageIsPascal(language) || 120 // Use Clang for Rust until there is a proper language plugin for it 121 language == eLanguageTypeRust || 122 language == eLanguageTypeExtRenderScript || 123 // Use Clang for D until there is a proper language plugin for it 124 language == eLanguageTypeD || 125 // Open Dylan compiler debug info is designed to be Clang-compatible 126 language == eLanguageTypeDylan; 127} 128 129// Checks whether m1 is an overload of m2 (as opposed to an override). This is 130// called by addOverridesForMethod to distinguish overrides (which share a 131// vtable entry) from overloads (which require distinct entries). 132bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) { 133 // FIXME: This should detect covariant return types, but currently doesn't. 134 lldbassert(&m1->getASTContext() == &m2->getASTContext() && 135 "Methods should have the same AST context"); 136 clang::ASTContext &context = m1->getASTContext(); 137 138 const auto *m1Type = llvm::cast<clang::FunctionProtoType>( 139 context.getCanonicalType(m1->getType())); 140 141 const auto *m2Type = llvm::cast<clang::FunctionProtoType>( 142 context.getCanonicalType(m2->getType())); 143 144 auto compareArgTypes = [&context](const clang::QualType &m1p, 145 const clang::QualType &m2p) { 146 return context.hasSameType(m1p.getUnqualifiedType(), 147 m2p.getUnqualifiedType()); 148 }; 149 150 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end() 151 // as a fourth parameter to std::equal(). 152 return (m1->getNumParams() != m2->getNumParams()) || 153 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(), 154 m2Type->param_type_begin(), compareArgTypes); 155} 156 157// If decl is a virtual method, walk the base classes looking for methods that 158// decl overrides. This table of overridden methods is used by IRGen to 159// determine the vtable layout for decl's parent class. 160void addOverridesForMethod(clang::CXXMethodDecl *decl) { 161 if (!decl->isVirtual()) 162 return; 163 164 clang::CXXBasePaths paths; 165 166 auto find_overridden_methods = 167 [decl](const clang::CXXBaseSpecifier *specifier, 168 clang::CXXBasePath &path) { 169 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>( 170 specifier->getType()->getAs<clang::RecordType>()->getDecl())) { 171 172 clang::DeclarationName name = decl->getDeclName(); 173 174 // If this is a destructor, check whether the base class destructor is 175 // virtual. 176 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName) 177 if (auto *baseDtorDecl = base_record->getDestructor()) { 178 if (baseDtorDecl->isVirtual()) { 179 path.Decls = baseDtorDecl; 180 return true; 181 } else 182 return false; 183 } 184 185 // Otherwise, search for name in the base class. 186 for (path.Decls = base_record->lookup(name); !path.Decls.empty(); 187 path.Decls = path.Decls.slice(1)) { 188 if (auto *method_decl = 189 llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front())) 190 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) { 191 path.Decls = method_decl; 192 return true; 193 } 194 } 195 } 196 197 return false; 198 }; 199 200 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) { 201 for (auto *overridden_decl : paths.found_decls()) 202 decl->addOverriddenMethod( 203 llvm::cast<clang::CXXMethodDecl>(overridden_decl)); 204 } 205} 206} 207 208static lldb::addr_t GetVTableAddress(Process &process, 209 VTableContextBase &vtable_ctx, 210 ValueObject &valobj, 211 const ASTRecordLayout &record_layout) { 212 // Retrieve type info 213 CompilerType pointee_type; 214 CompilerType this_type(valobj.GetCompilerType()); 215 uint32_t type_info = this_type.GetTypeInfo(&pointee_type); 216 if (!type_info) 217 return LLDB_INVALID_ADDRESS; 218 219 // Check if it's a pointer or reference 220 bool ptr_or_ref = false; 221 if (type_info & (eTypeIsPointer | eTypeIsReference)) { 222 ptr_or_ref = true; 223 type_info = pointee_type.GetTypeInfo(); 224 } 225 226 // We process only C++ classes 227 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus; 228 if ((type_info & cpp_class) != cpp_class) 229 return LLDB_INVALID_ADDRESS; 230 231 // Calculate offset to VTable pointer 232 lldb::offset_t vbtable_ptr_offset = 233 vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity() 234 : 0; 235 236 if (ptr_or_ref) { 237 // We have a pointer / ref to object, so read 238 // VTable pointer from process memory 239 240 if (valobj.GetAddressTypeOfChildren() != eAddressTypeLoad) 241 return LLDB_INVALID_ADDRESS; 242 243 auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS); 244 if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS) 245 return LLDB_INVALID_ADDRESS; 246 247 vbtable_ptr_addr += vbtable_ptr_offset; 248 249 Status err; 250 return process.ReadPointerFromMemory(vbtable_ptr_addr, err); 251 } 252 253 // We have an object already read from process memory, 254 // so just extract VTable pointer from it 255 256 DataExtractor data; 257 Status err; 258 auto size = valobj.GetData(data, err); 259 if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size) 260 return LLDB_INVALID_ADDRESS; 261 262 return data.GetPointer(&vbtable_ptr_offset); 263} 264 265static int64_t ReadVBaseOffsetFromVTable(Process &process, 266 VTableContextBase &vtable_ctx, 267 lldb::addr_t vtable_ptr, 268 const CXXRecordDecl *cxx_record_decl, 269 const CXXRecordDecl *base_class_decl) { 270 if (vtable_ctx.isMicrosoft()) { 271 clang::MicrosoftVTableContext &msoft_vtable_ctx = 272 static_cast<clang::MicrosoftVTableContext &>(vtable_ctx); 273 274 // Get the index into the virtual base table. The 275 // index is the index in uint32_t from vbtable_ptr 276 const unsigned vbtable_index = 277 msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl); 278 const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4; 279 Status err; 280 return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX, 281 err); 282 } 283 284 clang::ItaniumVTableContext &itanium_vtable_ctx = 285 static_cast<clang::ItaniumVTableContext &>(vtable_ctx); 286 287 clang::CharUnits base_offset_offset = 288 itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl, 289 base_class_decl); 290 const lldb::addr_t base_offset_addr = 291 vtable_ptr + base_offset_offset.getQuantity(); 292 const uint32_t base_offset_size = process.GetAddressByteSize(); 293 Status err; 294 return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size, 295 INT64_MAX, err); 296} 297 298static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx, 299 ValueObject &valobj, 300 const ASTRecordLayout &record_layout, 301 const CXXRecordDecl *cxx_record_decl, 302 const CXXRecordDecl *base_class_decl, 303 int32_t &bit_offset) { 304 ExecutionContext exe_ctx(valobj.GetExecutionContextRef()); 305 Process *process = exe_ctx.GetProcessPtr(); 306 if (!process) 307 return false; 308 309 lldb::addr_t vtable_ptr = 310 GetVTableAddress(*process, vtable_ctx, valobj, record_layout); 311 if (vtable_ptr == LLDB_INVALID_ADDRESS) 312 return false; 313 314 auto base_offset = ReadVBaseOffsetFromVTable( 315 *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl); 316 if (base_offset == INT64_MAX) 317 return false; 318 319 bit_offset = base_offset * 8; 320 321 return true; 322} 323 324typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *> 325 ClangASTMap; 326 327static ClangASTMap &GetASTMap() { 328 static ClangASTMap *g_map_ptr = nullptr; 329 static llvm::once_flag g_once_flag; 330 llvm::call_once(g_once_flag, []() { 331 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins 332 }); 333 return *g_map_ptr; 334} 335 336bool ClangASTContext::IsOperator(const char *name, 337 clang::OverloadedOperatorKind &op_kind) { 338 if (name == nullptr || name[0] == '\0') 339 return false; 340 341#define OPERATOR_PREFIX "operator" 342#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1) 343 344 const char *post_op_name = nullptr; 345 346 bool no_space = true; 347 348 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH)) 349 return false; 350 351 post_op_name = name + OPERATOR_PREFIX_LENGTH; 352 353 if (post_op_name[0] == ' ') { 354 post_op_name++; 355 no_space = false; 356 } 357 358#undef OPERATOR_PREFIX 359#undef OPERATOR_PREFIX_LENGTH 360 361 // This is an operator, set the overloaded operator kind to invalid in case 362 // this is a conversion operator... 363 op_kind = clang::NUM_OVERLOADED_OPERATORS; 364 365 switch (post_op_name[0]) { 366 default: 367 if (no_space) 368 return false; 369 break; 370 case 'n': 371 if (no_space) 372 return false; 373 if (strcmp(post_op_name, "new") == 0) 374 op_kind = clang::OO_New; 375 else if (strcmp(post_op_name, "new[]") == 0) 376 op_kind = clang::OO_Array_New; 377 break; 378 379 case 'd': 380 if (no_space) 381 return false; 382 if (strcmp(post_op_name, "delete") == 0) 383 op_kind = clang::OO_Delete; 384 else if (strcmp(post_op_name, "delete[]") == 0) 385 op_kind = clang::OO_Array_Delete; 386 break; 387 388 case '+': 389 if (post_op_name[1] == '\0') 390 op_kind = clang::OO_Plus; 391 else if (post_op_name[2] == '\0') { 392 if (post_op_name[1] == '=') 393 op_kind = clang::OO_PlusEqual; 394 else if (post_op_name[1] == '+') 395 op_kind = clang::OO_PlusPlus; 396 } 397 break; 398 399 case '-': 400 if (post_op_name[1] == '\0') 401 op_kind = clang::OO_Minus; 402 else if (post_op_name[2] == '\0') { 403 switch (post_op_name[1]) { 404 case '=': 405 op_kind = clang::OO_MinusEqual; 406 break; 407 case '-': 408 op_kind = clang::OO_MinusMinus; 409 break; 410 case '>': 411 op_kind = clang::OO_Arrow; 412 break; 413 } 414 } else if (post_op_name[3] == '\0') { 415 if (post_op_name[2] == '*') 416 op_kind = clang::OO_ArrowStar; 417 break; 418 } 419 break; 420 421 case '*': 422 if (post_op_name[1] == '\0') 423 op_kind = clang::OO_Star; 424 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 425 op_kind = clang::OO_StarEqual; 426 break; 427 428 case '/': 429 if (post_op_name[1] == '\0') 430 op_kind = clang::OO_Slash; 431 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 432 op_kind = clang::OO_SlashEqual; 433 break; 434 435 case '%': 436 if (post_op_name[1] == '\0') 437 op_kind = clang::OO_Percent; 438 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 439 op_kind = clang::OO_PercentEqual; 440 break; 441 442 case '^': 443 if (post_op_name[1] == '\0') 444 op_kind = clang::OO_Caret; 445 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 446 op_kind = clang::OO_CaretEqual; 447 break; 448 449 case '&': 450 if (post_op_name[1] == '\0') 451 op_kind = clang::OO_Amp; 452 else if (post_op_name[2] == '\0') { 453 switch (post_op_name[1]) { 454 case '=': 455 op_kind = clang::OO_AmpEqual; 456 break; 457 case '&': 458 op_kind = clang::OO_AmpAmp; 459 break; 460 } 461 } 462 break; 463 464 case '|': 465 if (post_op_name[1] == '\0') 466 op_kind = clang::OO_Pipe; 467 else if (post_op_name[2] == '\0') { 468 switch (post_op_name[1]) { 469 case '=': 470 op_kind = clang::OO_PipeEqual; 471 break; 472 case '|': 473 op_kind = clang::OO_PipePipe; 474 break; 475 } 476 } 477 break; 478 479 case '~': 480 if (post_op_name[1] == '\0') 481 op_kind = clang::OO_Tilde; 482 break; 483 484 case '!': 485 if (post_op_name[1] == '\0') 486 op_kind = clang::OO_Exclaim; 487 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 488 op_kind = clang::OO_ExclaimEqual; 489 break; 490 491 case '=': 492 if (post_op_name[1] == '\0') 493 op_kind = clang::OO_Equal; 494 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 495 op_kind = clang::OO_EqualEqual; 496 break; 497 498 case '<': 499 if (post_op_name[1] == '\0') 500 op_kind = clang::OO_Less; 501 else if (post_op_name[2] == '\0') { 502 switch (post_op_name[1]) { 503 case '<': 504 op_kind = clang::OO_LessLess; 505 break; 506 case '=': 507 op_kind = clang::OO_LessEqual; 508 break; 509 } 510 } else if (post_op_name[3] == '\0') { 511 if (post_op_name[2] == '=') 512 op_kind = clang::OO_LessLessEqual; 513 } 514 break; 515 516 case '>': 517 if (post_op_name[1] == '\0') 518 op_kind = clang::OO_Greater; 519 else if (post_op_name[2] == '\0') { 520 switch (post_op_name[1]) { 521 case '>': 522 op_kind = clang::OO_GreaterGreater; 523 break; 524 case '=': 525 op_kind = clang::OO_GreaterEqual; 526 break; 527 } 528 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' && 529 post_op_name[3] == '\0') { 530 op_kind = clang::OO_GreaterGreaterEqual; 531 } 532 break; 533 534 case ',': 535 if (post_op_name[1] == '\0') 536 op_kind = clang::OO_Comma; 537 break; 538 539 case '(': 540 if (post_op_name[1] == ')' && post_op_name[2] == '\0') 541 op_kind = clang::OO_Call; 542 break; 543 544 case '[': 545 if (post_op_name[1] == ']' && post_op_name[2] == '\0') 546 op_kind = clang::OO_Subscript; 547 break; 548 } 549 550 return true; 551} 552 553clang::AccessSpecifier 554ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) { 555 switch (access) { 556 default: 557 break; 558 case eAccessNone: 559 return AS_none; 560 case eAccessPublic: 561 return AS_public; 562 case eAccessPrivate: 563 return AS_private; 564 case eAccessProtected: 565 return AS_protected; 566 } 567 return AS_none; 568} 569 570static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) { 571 // FIXME: Cleanup per-file based stuff. 572 573 // Set some properties which depend solely on the input kind; it would be 574 // nice to move these to the language standard, and have the driver resolve 575 // the input kind + language standard. 576 if (IK.getLanguage() == InputKind::Asm) { 577 Opts.AsmPreprocessor = 1; 578 } else if (IK.isObjectiveC()) { 579 Opts.ObjC = 1; 580 } 581 582 LangStandard::Kind LangStd = LangStandard::lang_unspecified; 583 584 if (LangStd == LangStandard::lang_unspecified) { 585 // Based on the base language, pick one. 586 switch (IK.getLanguage()) { 587 case InputKind::Unknown: 588 case InputKind::LLVM_IR: 589 case InputKind::RenderScript: 590 llvm_unreachable("Invalid input kind!"); 591 case InputKind::OpenCL: 592 LangStd = LangStandard::lang_opencl10; 593 break; 594 case InputKind::CUDA: 595 LangStd = LangStandard::lang_cuda; 596 break; 597 case InputKind::Asm: 598 case InputKind::C: 599 case InputKind::ObjC: 600 LangStd = LangStandard::lang_gnu99; 601 break; 602 case InputKind::CXX: 603 case InputKind::ObjCXX: 604 LangStd = LangStandard::lang_gnucxx98; 605 break; 606 case InputKind::HIP: 607 LangStd = LangStandard::lang_hip; 608 break; 609 } 610 } 611 612 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd); 613 Opts.LineComment = Std.hasLineComments(); 614 Opts.C99 = Std.isC99(); 615 Opts.CPlusPlus = Std.isCPlusPlus(); 616 Opts.CPlusPlus11 = Std.isCPlusPlus11(); 617 Opts.Digraphs = Std.hasDigraphs(); 618 Opts.GNUMode = Std.isGNUMode(); 619 Opts.GNUInline = !Std.isC99(); 620 Opts.HexFloats = Std.hasHexFloats(); 621 Opts.ImplicitInt = Std.hasImplicitInt(); 622 623 Opts.WChar = true; 624 625 // OpenCL has some additional defaults. 626 if (LangStd == LangStandard::lang_opencl10) { 627 Opts.OpenCL = 1; 628 Opts.AltiVec = 1; 629 Opts.CXXOperatorNames = 1; 630 Opts.LaxVectorConversions = 1; 631 } 632 633 // OpenCL and C++ both have bool, true, false keywords. 634 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus; 635 636 Opts.setValueVisibilityMode(DefaultVisibility); 637 638 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is 639 // specified, or -std is set to a conforming mode. 640 Opts.Trigraphs = !Opts.GNUMode; 641 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault(); 642 Opts.OptimizeSize = 0; 643 644 // FIXME: Eliminate this dependency. 645 // unsigned Opt = 646 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags); 647 // Opts.Optimize = Opt != 0; 648 unsigned Opt = 0; 649 650 // This is the __NO_INLINE__ define, which just depends on things like the 651 // optimization level and -fno-inline, not actually whether the backend has 652 // inlining enabled. 653 // 654 // FIXME: This is affected by other options (-fno-inline). 655 Opts.NoInlineDefine = !Opt; 656} 657 658ClangASTContext::ClangASTContext(const char *target_triple) 659 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(), 660 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(), 661 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(), 662 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr), 663 m_callback_objc_decl(nullptr), m_callback_baton(nullptr), 664 m_pointer_byte_size(0), m_ast_owned(false) { 665 if (target_triple && target_triple[0]) 666 SetTargetTriple(target_triple); 667} 668 669//---------------------------------------------------------------------- 670// Destructor 671//---------------------------------------------------------------------- 672ClangASTContext::~ClangASTContext() { Finalize(); } 673 674ConstString ClangASTContext::GetPluginNameStatic() { 675 return ConstString("clang"); 676} 677 678ConstString ClangASTContext::GetPluginName() { 679 return ClangASTContext::GetPluginNameStatic(); 680} 681 682uint32_t ClangASTContext::GetPluginVersion() { return 1; } 683 684lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language, 685 lldb_private::Module *module, 686 Target *target) { 687 if (ClangASTContextSupportsLanguage(language)) { 688 ArchSpec arch; 689 if (module) 690 arch = module->GetArchitecture(); 691 else if (target) 692 arch = target->GetArchitecture(); 693 694 if (arch.IsValid()) { 695 ArchSpec fixed_arch = arch; 696 // LLVM wants this to be set to iOS or MacOSX; if we're working on 697 // a bare-boards type image, change the triple for llvm's benefit. 698 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple && 699 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) { 700 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm || 701 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 || 702 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) { 703 fixed_arch.GetTriple().setOS(llvm::Triple::IOS); 704 } else { 705 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX); 706 } 707 } 708 709 if (module) { 710 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext); 711 if (ast_sp) { 712 ast_sp->SetArchitecture(fixed_arch); 713 } 714 return ast_sp; 715 } else if (target && target->IsValid()) { 716 std::shared_ptr<ClangASTContextForExpressions> ast_sp( 717 new ClangASTContextForExpressions(*target)); 718 if (ast_sp) { 719 ast_sp->SetArchitecture(fixed_arch); 720 ast_sp->m_scratch_ast_source_ap.reset( 721 new ClangASTSource(target->shared_from_this())); 722 lldbassert(ast_sp->getFileManager()); 723 ast_sp->m_scratch_ast_source_ap->InstallASTContext( 724 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true); 725 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source( 726 ast_sp->m_scratch_ast_source_ap->CreateProxy()); 727 ast_sp->SetExternalSource(proxy_ast_source); 728 return ast_sp; 729 } 730 } 731 } 732 } 733 return lldb::TypeSystemSP(); 734} 735 736void ClangASTContext::EnumerateSupportedLanguages( 737 std::set<lldb::LanguageType> &languages_for_types, 738 std::set<lldb::LanguageType> &languages_for_expressions) { 739 static std::vector<lldb::LanguageType> s_supported_languages_for_types( 740 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11, 741 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99, 742 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus, 743 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11, 744 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14}); 745 746 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions( 747 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus, 748 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11, 749 lldb::eLanguageTypeC_plus_plus_14}); 750 751 languages_for_types.insert(s_supported_languages_for_types.begin(), 752 s_supported_languages_for_types.end()); 753 languages_for_expressions.insert( 754 s_supported_languages_for_expressions.begin(), 755 s_supported_languages_for_expressions.end()); 756} 757 758void ClangASTContext::Initialize() { 759 PluginManager::RegisterPlugin(GetPluginNameStatic(), 760 "clang base AST context plug-in", 761 CreateInstance, EnumerateSupportedLanguages); 762} 763 764void ClangASTContext::Terminate() { 765 PluginManager::UnregisterPlugin(CreateInstance); 766} 767 768void ClangASTContext::Finalize() { 769 if (m_ast_ap.get()) { 770 GetASTMap().Erase(m_ast_ap.get()); 771 if (!m_ast_owned) 772 m_ast_ap.release(); 773 } 774 775 m_builtins_ap.reset(); 776 m_selector_table_ap.reset(); 777 m_identifier_table_ap.reset(); 778 m_target_info_ap.reset(); 779 m_target_options_rp.reset(); 780 m_diagnostics_engine_ap.reset(); 781 m_source_manager_ap.reset(); 782 m_language_options_ap.reset(); 783 m_ast_ap.reset(); 784 m_scratch_ast_source_ap.reset(); 785} 786 787void ClangASTContext::Clear() { 788 m_ast_ap.reset(); 789 m_language_options_ap.reset(); 790 m_source_manager_ap.reset(); 791 m_diagnostics_engine_ap.reset(); 792 m_target_options_rp.reset(); 793 m_target_info_ap.reset(); 794 m_identifier_table_ap.reset(); 795 m_selector_table_ap.reset(); 796 m_builtins_ap.reset(); 797 m_pointer_byte_size = 0; 798} 799 800const char *ClangASTContext::GetTargetTriple() { 801 return m_target_triple.c_str(); 802} 803 804void ClangASTContext::SetTargetTriple(const char *target_triple) { 805 Clear(); 806 m_target_triple.assign(target_triple); 807} 808 809void ClangASTContext::SetArchitecture(const ArchSpec &arch) { 810 SetTargetTriple(arch.GetTriple().str().c_str()); 811} 812 813bool ClangASTContext::HasExternalSource() { 814 ASTContext *ast = getASTContext(); 815 if (ast) 816 return ast->getExternalSource() != nullptr; 817 return false; 818} 819 820void ClangASTContext::SetExternalSource( 821 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) { 822 ASTContext *ast = getASTContext(); 823 if (ast) { 824 ast->setExternalSource(ast_source_ap); 825 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true); 826 } 827} 828 829void ClangASTContext::RemoveExternalSource() { 830 ASTContext *ast = getASTContext(); 831 832 if (ast) { 833 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap; 834 ast->setExternalSource(empty_ast_source_ap); 835 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false); 836 } 837} 838 839void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) { 840 if (!m_ast_owned) { 841 m_ast_ap.release(); 842 } 843 m_ast_owned = false; 844 m_ast_ap.reset(ast_ctx); 845 GetASTMap().Insert(ast_ctx, this); 846} 847 848ASTContext *ClangASTContext::getASTContext() { 849 if (m_ast_ap.get() == nullptr) { 850 m_ast_owned = true; 851 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(), 852 *getIdentifierTable(), *getSelectorTable(), 853 *getBuiltinContext())); 854 855 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false); 856 857 // This can be NULL if we don't know anything about the architecture or if 858 // the target for an architecture isn't enabled in the llvm/clang that we 859 // built 860 TargetInfo *target_info = getTargetInfo(); 861 if (target_info) 862 m_ast_ap->InitBuiltinTypes(*target_info); 863 864 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) { 865 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage(); 866 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage(); 867 } 868 869 GetASTMap().Insert(m_ast_ap.get(), this); 870 871 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap( 872 new ClangExternalASTSourceCallbacks( 873 ClangASTContext::CompleteTagDecl, 874 ClangASTContext::CompleteObjCInterfaceDecl, nullptr, 875 ClangASTContext::LayoutRecordType, this)); 876 SetExternalSource(ast_source_ap); 877 } 878 return m_ast_ap.get(); 879} 880 881ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) { 882 ClangASTContext *clang_ast = GetASTMap().Lookup(ast); 883 return clang_ast; 884} 885 886Builtin::Context *ClangASTContext::getBuiltinContext() { 887 if (m_builtins_ap.get() == nullptr) 888 m_builtins_ap.reset(new Builtin::Context()); 889 return m_builtins_ap.get(); 890} 891 892IdentifierTable *ClangASTContext::getIdentifierTable() { 893 if (m_identifier_table_ap.get() == nullptr) 894 m_identifier_table_ap.reset( 895 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr)); 896 return m_identifier_table_ap.get(); 897} 898 899LangOptions *ClangASTContext::getLanguageOptions() { 900 if (m_language_options_ap.get() == nullptr) { 901 m_language_options_ap.reset(new LangOptions()); 902 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple()); 903 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX); 904 } 905 return m_language_options_ap.get(); 906} 907 908SelectorTable *ClangASTContext::getSelectorTable() { 909 if (m_selector_table_ap.get() == nullptr) 910 m_selector_table_ap.reset(new SelectorTable()); 911 return m_selector_table_ap.get(); 912} 913 914clang::FileManager *ClangASTContext::getFileManager() { 915 if (m_file_manager_ap.get() == nullptr) { 916 clang::FileSystemOptions file_system_options; 917 m_file_manager_ap.reset(new clang::FileManager(file_system_options)); 918 } 919 return m_file_manager_ap.get(); 920} 921 922clang::SourceManager *ClangASTContext::getSourceManager() { 923 if (m_source_manager_ap.get() == nullptr) 924 m_source_manager_ap.reset( 925 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager())); 926 return m_source_manager_ap.get(); 927} 928 929clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() { 930 if (m_diagnostics_engine_ap.get() == nullptr) { 931 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs()); 932 m_diagnostics_engine_ap.reset( 933 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions())); 934 } 935 return m_diagnostics_engine_ap.get(); 936} 937 938clang::MangleContext *ClangASTContext::getMangleContext() { 939 if (m_mangle_ctx_ap.get() == nullptr) 940 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext()); 941 return m_mangle_ctx_ap.get(); 942} 943 944class NullDiagnosticConsumer : public DiagnosticConsumer { 945public: 946 NullDiagnosticConsumer() { 947 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS); 948 } 949 950 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, 951 const clang::Diagnostic &info) { 952 if (m_log) { 953 llvm::SmallVector<char, 32> diag_str(10); 954 info.FormatDiagnostic(diag_str); 955 diag_str.push_back('\0'); 956 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data()); 957 } 958 } 959 960 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { 961 return new NullDiagnosticConsumer(); 962 } 963 964private: 965 Log *m_log; 966}; 967 968DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() { 969 if (m_diagnostic_consumer_ap.get() == nullptr) 970 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer); 971 972 return m_diagnostic_consumer_ap.get(); 973} 974 975std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() { 976 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) { 977 m_target_options_rp = std::make_shared<clang::TargetOptions>(); 978 if (m_target_options_rp.get() != nullptr) 979 m_target_options_rp->Triple = m_target_triple; 980 } 981 return m_target_options_rp; 982} 983 984TargetInfo *ClangASTContext::getTargetInfo() { 985 // target_triple should be something like "x86_64-apple-macosx" 986 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty()) 987 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), 988 getTargetOptions())); 989 return m_target_info_ap.get(); 990} 991 992#pragma mark Basic Types 993 994static inline bool QualTypeMatchesBitSize(const uint64_t bit_size, 995 ASTContext *ast, QualType qual_type) { 996 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type); 997 return qual_type_bit_size == bit_size; 998} 999 1000CompilerType 1001ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding, 1002 size_t bit_size) { 1003 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize( 1004 getASTContext(), encoding, bit_size); 1005} 1006 1007CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize( 1008 ASTContext *ast, Encoding encoding, uint32_t bit_size) { 1009 if (!ast) 1010 return CompilerType(); 1011 switch (encoding) { 1012 case eEncodingInvalid: 1013 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy)) 1014 return CompilerType(ast, ast->VoidPtrTy); 1015 break; 1016 1017 case eEncodingUint: 1018 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) 1019 return CompilerType(ast, ast->UnsignedCharTy); 1020 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) 1021 return CompilerType(ast, ast->UnsignedShortTy); 1022 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) 1023 return CompilerType(ast, ast->UnsignedIntTy); 1024 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy)) 1025 return CompilerType(ast, ast->UnsignedLongTy); 1026 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy)) 1027 return CompilerType(ast, ast->UnsignedLongLongTy); 1028 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty)) 1029 return CompilerType(ast, ast->UnsignedInt128Ty); 1030 break; 1031 1032 case eEncodingSint: 1033 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy)) 1034 return CompilerType(ast, ast->SignedCharTy); 1035 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy)) 1036 return CompilerType(ast, ast->ShortTy); 1037 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy)) 1038 return CompilerType(ast, ast->IntTy); 1039 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy)) 1040 return CompilerType(ast, ast->LongTy); 1041 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy)) 1042 return CompilerType(ast, ast->LongLongTy); 1043 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty)) 1044 return CompilerType(ast, ast->Int128Ty); 1045 break; 1046 1047 case eEncodingIEEE754: 1048 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy)) 1049 return CompilerType(ast, ast->FloatTy); 1050 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy)) 1051 return CompilerType(ast, ast->DoubleTy); 1052 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy)) 1053 return CompilerType(ast, ast->LongDoubleTy); 1054 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy)) 1055 return CompilerType(ast, ast->HalfTy); 1056 break; 1057 1058 case eEncodingVector: 1059 // Sanity check that bit_size is a multiple of 8's. 1060 if (bit_size && !(bit_size & 0x7u)) 1061 return CompilerType( 1062 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8)); 1063 break; 1064 } 1065 1066 return CompilerType(); 1067} 1068 1069lldb::BasicType 1070ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) { 1071 if (name) { 1072 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap; 1073 static TypeNameToBasicTypeMap g_type_map; 1074 static llvm::once_flag g_once_flag; 1075 llvm::call_once(g_once_flag, []() { 1076 // "void" 1077 g_type_map.Append(ConstString("void"), eBasicTypeVoid); 1078 1079 // "char" 1080 g_type_map.Append(ConstString("char"), eBasicTypeChar); 1081 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar); 1082 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar); 1083 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar); 1084 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar); 1085 g_type_map.Append(ConstString("unsigned wchar_t"), 1086 eBasicTypeUnsignedWChar); 1087 // "short" 1088 g_type_map.Append(ConstString("short"), eBasicTypeShort); 1089 g_type_map.Append(ConstString("short int"), eBasicTypeShort); 1090 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort); 1091 g_type_map.Append(ConstString("unsigned short int"), 1092 eBasicTypeUnsignedShort); 1093 1094 // "int" 1095 g_type_map.Append(ConstString("int"), eBasicTypeInt); 1096 g_type_map.Append(ConstString("signed int"), eBasicTypeInt); 1097 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt); 1098 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt); 1099 1100 // "long" 1101 g_type_map.Append(ConstString("long"), eBasicTypeLong); 1102 g_type_map.Append(ConstString("long int"), eBasicTypeLong); 1103 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong); 1104 g_type_map.Append(ConstString("unsigned long int"), 1105 eBasicTypeUnsignedLong); 1106 1107 // "long long" 1108 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong); 1109 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong); 1110 g_type_map.Append(ConstString("unsigned long long"), 1111 eBasicTypeUnsignedLongLong); 1112 g_type_map.Append(ConstString("unsigned long long int"), 1113 eBasicTypeUnsignedLongLong); 1114 1115 // "int128" 1116 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128); 1117 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128); 1118 1119 // Miscellaneous 1120 g_type_map.Append(ConstString("bool"), eBasicTypeBool); 1121 g_type_map.Append(ConstString("float"), eBasicTypeFloat); 1122 g_type_map.Append(ConstString("double"), eBasicTypeDouble); 1123 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble); 1124 g_type_map.Append(ConstString("id"), eBasicTypeObjCID); 1125 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel); 1126 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr); 1127 g_type_map.Sort(); 1128 }); 1129 1130 return g_type_map.Find(name, eBasicTypeInvalid); 1131 } 1132 return eBasicTypeInvalid; 1133} 1134 1135CompilerType ClangASTContext::GetBasicType(ASTContext *ast, 1136 const ConstString &name) { 1137 if (ast) { 1138 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name); 1139 return ClangASTContext::GetBasicType(ast, basic_type); 1140 } 1141 return CompilerType(); 1142} 1143 1144uint32_t ClangASTContext::GetPointerByteSize() { 1145 if (m_pointer_byte_size == 0) 1146 if (auto size = GetBasicType(lldb::eBasicTypeVoid) 1147 .GetPointerType() 1148 .GetByteSize(nullptr)) 1149 m_pointer_byte_size = *size; 1150 return m_pointer_byte_size; 1151} 1152 1153CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) { 1154 return GetBasicType(getASTContext(), basic_type); 1155} 1156 1157CompilerType ClangASTContext::GetBasicType(ASTContext *ast, 1158 lldb::BasicType basic_type) { 1159 if (!ast) 1160 return CompilerType(); 1161 lldb::opaque_compiler_type_t clang_type = 1162 GetOpaqueCompilerType(ast, basic_type); 1163 1164 if (clang_type) 1165 return CompilerType(GetASTContext(ast), clang_type); 1166 return CompilerType(); 1167} 1168 1169CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize( 1170 const char *type_name, uint32_t dw_ate, uint32_t bit_size) { 1171 ASTContext *ast = getASTContext(); 1172 1173#define streq(a, b) strcmp(a, b) == 0 1174 assert(ast != nullptr); 1175 if (ast) { 1176 switch (dw_ate) { 1177 default: 1178 break; 1179 1180 case DW_ATE_address: 1181 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy)) 1182 return CompilerType(ast, ast->VoidPtrTy); 1183 break; 1184 1185 case DW_ATE_boolean: 1186 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy)) 1187 return CompilerType(ast, ast->BoolTy); 1188 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) 1189 return CompilerType(ast, ast->UnsignedCharTy); 1190 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) 1191 return CompilerType(ast, ast->UnsignedShortTy); 1192 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) 1193 return CompilerType(ast, ast->UnsignedIntTy); 1194 break; 1195 1196 case DW_ATE_lo_user: 1197 // This has been seen to mean DW_AT_complex_integer 1198 if (type_name) { 1199 if (::strstr(type_name, "complex")) { 1200 CompilerType complex_int_clang_type = 1201 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed, 1202 bit_size / 2); 1203 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType( 1204 complex_int_clang_type))); 1205 } 1206 } 1207 break; 1208 1209 case DW_ATE_complex_float: 1210 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy)) 1211 return CompilerType(ast, ast->FloatComplexTy); 1212 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy)) 1213 return CompilerType(ast, ast->DoubleComplexTy); 1214 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy)) 1215 return CompilerType(ast, ast->LongDoubleComplexTy); 1216 else { 1217 CompilerType complex_float_clang_type = 1218 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float, 1219 bit_size / 2); 1220 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType( 1221 complex_float_clang_type))); 1222 } 1223 break; 1224 1225 case DW_ATE_float: 1226 if (streq(type_name, "float") && 1227 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy)) 1228 return CompilerType(ast, ast->FloatTy); 1229 if (streq(type_name, "double") && 1230 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy)) 1231 return CompilerType(ast, ast->DoubleTy); 1232 if (streq(type_name, "long double") && 1233 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy)) 1234 return CompilerType(ast, ast->LongDoubleTy); 1235 // Fall back to not requiring a name match 1236 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy)) 1237 return CompilerType(ast, ast->FloatTy); 1238 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy)) 1239 return CompilerType(ast, ast->DoubleTy); 1240 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy)) 1241 return CompilerType(ast, ast->LongDoubleTy); 1242 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy)) 1243 return CompilerType(ast, ast->HalfTy); 1244 break; 1245 1246 case DW_ATE_signed: 1247 if (type_name) { 1248 if (streq(type_name, "wchar_t") && 1249 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) && 1250 (getTargetInfo() && 1251 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType()))) 1252 return CompilerType(ast, ast->WCharTy); 1253 if (streq(type_name, "void") && 1254 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy)) 1255 return CompilerType(ast, ast->VoidTy); 1256 if (strstr(type_name, "long long") && 1257 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy)) 1258 return CompilerType(ast, ast->LongLongTy); 1259 if (strstr(type_name, "long") && 1260 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy)) 1261 return CompilerType(ast, ast->LongTy); 1262 if (strstr(type_name, "short") && 1263 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy)) 1264 return CompilerType(ast, ast->ShortTy); 1265 if (strstr(type_name, "char")) { 1266 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) 1267 return CompilerType(ast, ast->CharTy); 1268 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy)) 1269 return CompilerType(ast, ast->SignedCharTy); 1270 } 1271 if (strstr(type_name, "int")) { 1272 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy)) 1273 return CompilerType(ast, ast->IntTy); 1274 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty)) 1275 return CompilerType(ast, ast->Int128Ty); 1276 } 1277 } 1278 // We weren't able to match up a type name, just search by size 1279 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) 1280 return CompilerType(ast, ast->CharTy); 1281 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy)) 1282 return CompilerType(ast, ast->ShortTy); 1283 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy)) 1284 return CompilerType(ast, ast->IntTy); 1285 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy)) 1286 return CompilerType(ast, ast->LongTy); 1287 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy)) 1288 return CompilerType(ast, ast->LongLongTy); 1289 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty)) 1290 return CompilerType(ast, ast->Int128Ty); 1291 break; 1292 1293 case DW_ATE_signed_char: 1294 if (ast->getLangOpts().CharIsSigned && type_name && 1295 streq(type_name, "char")) { 1296 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) 1297 return CompilerType(ast, ast->CharTy); 1298 } 1299 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy)) 1300 return CompilerType(ast, ast->SignedCharTy); 1301 break; 1302 1303 case DW_ATE_unsigned: 1304 if (type_name) { 1305 if (streq(type_name, "wchar_t")) { 1306 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) { 1307 if (!(getTargetInfo() && 1308 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType()))) 1309 return CompilerType(ast, ast->WCharTy); 1310 } 1311 } 1312 if (strstr(type_name, "long long")) { 1313 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy)) 1314 return CompilerType(ast, ast->UnsignedLongLongTy); 1315 } else if (strstr(type_name, "long")) { 1316 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy)) 1317 return CompilerType(ast, ast->UnsignedLongTy); 1318 } else if (strstr(type_name, "short")) { 1319 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) 1320 return CompilerType(ast, ast->UnsignedShortTy); 1321 } else if (strstr(type_name, "char")) { 1322 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) 1323 return CompilerType(ast, ast->UnsignedCharTy); 1324 } else if (strstr(type_name, "int")) { 1325 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) 1326 return CompilerType(ast, ast->UnsignedIntTy); 1327 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty)) 1328 return CompilerType(ast, ast->UnsignedInt128Ty); 1329 } 1330 } 1331 // We weren't able to match up a type name, just search by size 1332 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) 1333 return CompilerType(ast, ast->UnsignedCharTy); 1334 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) 1335 return CompilerType(ast, ast->UnsignedShortTy); 1336 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) 1337 return CompilerType(ast, ast->UnsignedIntTy); 1338 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy)) 1339 return CompilerType(ast, ast->UnsignedLongTy); 1340 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy)) 1341 return CompilerType(ast, ast->UnsignedLongLongTy); 1342 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty)) 1343 return CompilerType(ast, ast->UnsignedInt128Ty); 1344 break; 1345 1346 case DW_ATE_unsigned_char: 1347 if (!ast->getLangOpts().CharIsSigned && type_name && 1348 streq(type_name, "char")) { 1349 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) 1350 return CompilerType(ast, ast->CharTy); 1351 } 1352 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) 1353 return CompilerType(ast, ast->UnsignedCharTy); 1354 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) 1355 return CompilerType(ast, ast->UnsignedShortTy); 1356 break; 1357 1358 case DW_ATE_imaginary_float: 1359 break; 1360 1361 case DW_ATE_UTF: 1362 if (type_name) { 1363 if (streq(type_name, "char16_t")) { 1364 return CompilerType(ast, ast->Char16Ty); 1365 } else if (streq(type_name, "char32_t")) { 1366 return CompilerType(ast, ast->Char32Ty); 1367 } 1368 } 1369 break; 1370 } 1371 } 1372 // This assert should fire for anything that we don't catch above so we know 1373 // to fix any issues we run into. 1374 if (type_name) { 1375 Host::SystemLog(Host::eSystemLogError, "error: need to add support for " 1376 "DW_TAG_base_type '%s' encoded with " 1377 "DW_ATE = 0x%x, bit_size = %u\n", 1378 type_name, dw_ate, bit_size); 1379 } else { 1380 Host::SystemLog(Host::eSystemLogError, "error: need to add support for " 1381 "DW_TAG_base_type encoded with " 1382 "DW_ATE = 0x%x, bit_size = %u\n", 1383 dw_ate, bit_size); 1384 } 1385 return CompilerType(); 1386} 1387 1388CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) { 1389 if (ast) 1390 return CompilerType(ast, ast->UnknownAnyTy); 1391 return CompilerType(); 1392} 1393 1394CompilerType ClangASTContext::GetCStringType(bool is_const) { 1395 ASTContext *ast = getASTContext(); 1396 QualType char_type(ast->CharTy); 1397 1398 if (is_const) 1399 char_type.addConst(); 1400 1401 return CompilerType(ast, ast->getPointerType(char_type)); 1402} 1403 1404clang::DeclContext * 1405ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) { 1406 return ast->getTranslationUnitDecl(); 1407} 1408 1409clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast, 1410 clang::Decl *source_decl) { 1411 FileSystemOptions file_system_options; 1412 FileManager file_manager(file_system_options); 1413 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false); 1414 1415 return importer.Import(source_decl); 1416} 1417 1418bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2, 1419 bool ignore_qualifiers) { 1420 ClangASTContext *ast = 1421 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem()); 1422 if (!ast || ast != type2.GetTypeSystem()) 1423 return false; 1424 1425 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType()) 1426 return true; 1427 1428 QualType type1_qual = ClangUtil::GetQualType(type1); 1429 QualType type2_qual = ClangUtil::GetQualType(type2); 1430 1431 if (ignore_qualifiers) { 1432 type1_qual = type1_qual.getUnqualifiedType(); 1433 type2_qual = type2_qual.getUnqualifiedType(); 1434 } 1435 1436 return ast->getASTContext()->hasSameType(type1_qual, type2_qual); 1437} 1438 1439CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) { 1440 if (clang::ObjCInterfaceDecl *interface_decl = 1441 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) 1442 return GetTypeForDecl(interface_decl); 1443 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) 1444 return GetTypeForDecl(tag_decl); 1445 return CompilerType(); 1446} 1447 1448CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) { 1449 // No need to call the getASTContext() accessor (which can create the AST if 1450 // it isn't created yet, because we can't have created a decl in this 1451 // AST if our AST didn't already exist... 1452 ASTContext *ast = &decl->getASTContext(); 1453 if (ast) 1454 return CompilerType(ast, ast->getTagDeclType(decl)); 1455 return CompilerType(); 1456} 1457 1458CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) { 1459 // No need to call the getASTContext() accessor (which can create the AST if 1460 // it isn't created yet, because we can't have created a decl in this 1461 // AST if our AST didn't already exist... 1462 ASTContext *ast = &decl->getASTContext(); 1463 if (ast) 1464 return CompilerType(ast, ast->getObjCInterfaceType(decl)); 1465 return CompilerType(); 1466} 1467 1468#pragma mark Structure, Unions, Classes 1469 1470CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx, 1471 AccessType access_type, 1472 const char *name, int kind, 1473 LanguageType language, 1474 ClangASTMetadata *metadata) { 1475 ASTContext *ast = getASTContext(); 1476 assert(ast != nullptr); 1477 1478 if (decl_ctx == nullptr) 1479 decl_ctx = ast->getTranslationUnitDecl(); 1480 1481 if (language == eLanguageTypeObjC || 1482 language == eLanguageTypeObjC_plus_plus) { 1483 bool isForwardDecl = true; 1484 bool isInternal = false; 1485 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata); 1486 } 1487 1488 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and 1489 // we will need to update this code. I was told to currently always use the 1490 // CXXRecordDecl class since we often don't know from debug information if 1491 // something is struct or a class, so we default to always use the more 1492 // complete definition just in case. 1493 1494 bool is_anonymous = (!name) || (!name[0]); 1495 1496 CXXRecordDecl *decl = CXXRecordDecl::Create( 1497 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(), 1498 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name)); 1499 1500 if (is_anonymous) 1501 decl->setAnonymousStructOrUnion(true); 1502 1503 if (decl) { 1504 if (metadata) 1505 SetMetadata(ast, decl, *metadata); 1506 1507 if (access_type != eAccessNone) 1508 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type)); 1509 1510 if (decl_ctx) 1511 decl_ctx->addDecl(decl); 1512 1513 return CompilerType(ast, ast->getTagDeclType(decl)); 1514 } 1515 return CompilerType(); 1516} 1517 1518namespace { 1519 bool IsValueParam(const clang::TemplateArgument &argument) { 1520 return argument.getKind() == TemplateArgument::Integral; 1521 } 1522} 1523 1524static TemplateParameterList *CreateTemplateParameterList( 1525 ASTContext *ast, 1526 const ClangASTContext::TemplateParameterInfos &template_param_infos, 1527 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) { 1528 const bool parameter_pack = false; 1529 const bool is_typename = false; 1530 const unsigned depth = 0; 1531 const size_t num_template_params = template_param_infos.args.size(); 1532 DeclContext *const decl_context = 1533 ast->getTranslationUnitDecl(); // Is this the right decl context?, 1534 for (size_t i = 0; i < num_template_params; ++i) { 1535 const char *name = template_param_infos.names[i]; 1536 1537 IdentifierInfo *identifier_info = nullptr; 1538 if (name && name[0]) 1539 identifier_info = &ast->Idents.get(name); 1540 if (IsValueParam(template_param_infos.args[i])) { 1541 template_param_decls.push_back(NonTypeTemplateParmDecl::Create( 1542 *ast, decl_context, 1543 SourceLocation(), SourceLocation(), depth, i, identifier_info, 1544 template_param_infos.args[i].getIntegralType(), parameter_pack, 1545 nullptr)); 1546 1547 } else { 1548 template_param_decls.push_back(TemplateTypeParmDecl::Create( 1549 *ast, decl_context, 1550 SourceLocation(), SourceLocation(), depth, i, identifier_info, 1551 is_typename, parameter_pack)); 1552 } 1553 } 1554 1555 if (template_param_infos.packed_args && 1556 template_param_infos.packed_args->args.size()) { 1557 IdentifierInfo *identifier_info = nullptr; 1558 if (template_param_infos.pack_name && template_param_infos.pack_name[0]) 1559 identifier_info = &ast->Idents.get(template_param_infos.pack_name); 1560 const bool parameter_pack_true = true; 1561 if (IsValueParam(template_param_infos.packed_args->args[0])) { 1562 template_param_decls.push_back(NonTypeTemplateParmDecl::Create( 1563 *ast, decl_context, 1564 SourceLocation(), SourceLocation(), depth, num_template_params, 1565 identifier_info, 1566 template_param_infos.packed_args->args[0].getIntegralType(), 1567 parameter_pack_true, nullptr)); 1568 } else { 1569 template_param_decls.push_back(TemplateTypeParmDecl::Create( 1570 *ast, decl_context, 1571 SourceLocation(), SourceLocation(), depth, num_template_params, 1572 identifier_info, 1573 is_typename, parameter_pack_true)); 1574 } 1575 } 1576 clang::Expr *const requires_clause = nullptr; // TODO: Concepts 1577 TemplateParameterList *template_param_list = TemplateParameterList::Create( 1578 *ast, SourceLocation(), SourceLocation(), template_param_decls, 1579 SourceLocation(), requires_clause); 1580 return template_param_list; 1581} 1582 1583clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl( 1584 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl, 1585 const char *name, const TemplateParameterInfos &template_param_infos) { 1586 // /// Create a function template node. 1587 ASTContext *ast = getASTContext(); 1588 1589 llvm::SmallVector<NamedDecl *, 8> template_param_decls; 1590 1591 TemplateParameterList *template_param_list = CreateTemplateParameterList( 1592 ast, template_param_infos, template_param_decls); 1593 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create( 1594 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(), 1595 template_param_list, func_decl); 1596 1597 for (size_t i = 0, template_param_decl_count = template_param_decls.size(); 1598 i < template_param_decl_count; ++i) { 1599 // TODO: verify which decl context we should put template_param_decls into.. 1600 template_param_decls[i]->setDeclContext(func_decl); 1601 } 1602 1603 return func_tmpl_decl; 1604} 1605 1606void ClangASTContext::CreateFunctionTemplateSpecializationInfo( 1607 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl, 1608 const TemplateParameterInfos &infos) { 1609 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args); 1610 1611 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args, 1612 nullptr); 1613} 1614 1615ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl( 1616 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name, 1617 int kind, const TemplateParameterInfos &template_param_infos) { 1618 ASTContext *ast = getASTContext(); 1619 1620 ClassTemplateDecl *class_template_decl = nullptr; 1621 if (decl_ctx == nullptr) 1622 decl_ctx = ast->getTranslationUnitDecl(); 1623 1624 IdentifierInfo &identifier_info = ast->Idents.get(class_name); 1625 DeclarationName decl_name(&identifier_info); 1626 1627 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); 1628 1629 for (NamedDecl *decl : result) { 1630 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl); 1631 if (class_template_decl) 1632 return class_template_decl; 1633 } 1634 1635 llvm::SmallVector<NamedDecl *, 8> template_param_decls; 1636 1637 TemplateParameterList *template_param_list = CreateTemplateParameterList( 1638 ast, template_param_infos, template_param_decls); 1639 1640 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create( 1641 *ast, (TagDecl::TagKind)kind, 1642 decl_ctx, // What decl context do we use here? TU? The actual decl 1643 // context? 1644 SourceLocation(), SourceLocation(), &identifier_info); 1645 1646 for (size_t i = 0, template_param_decl_count = template_param_decls.size(); 1647 i < template_param_decl_count; ++i) { 1648 template_param_decls[i]->setDeclContext(template_cxx_decl); 1649 } 1650 1651 // With templated classes, we say that a class is templated with 1652 // specializations, but that the bare class has no functions. 1653 // template_cxx_decl->startDefinition(); 1654 // template_cxx_decl->completeDefinition(); 1655 1656 class_template_decl = ClassTemplateDecl::Create( 1657 *ast, 1658 decl_ctx, // What decl context do we use here? TU? The actual decl 1659 // context? 1660 SourceLocation(), decl_name, template_param_list, template_cxx_decl); 1661 1662 if (class_template_decl) { 1663 if (access_type != eAccessNone) 1664 class_template_decl->setAccess( 1665 ConvertAccessTypeToAccessSpecifier(access_type)); 1666 1667 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx)) 1668 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl)); 1669 1670 decl_ctx->addDecl(class_template_decl); 1671 1672#ifdef LLDB_CONFIGURATION_DEBUG 1673 VerifyDecl(class_template_decl); 1674#endif 1675 } 1676 1677 return class_template_decl; 1678} 1679 1680TemplateTemplateParmDecl * 1681ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) { 1682 ASTContext *ast = getASTContext(); 1683 1684 auto *decl_ctx = ast->getTranslationUnitDecl(); 1685 1686 IdentifierInfo &identifier_info = ast->Idents.get(template_name); 1687 llvm::SmallVector<NamedDecl *, 8> template_param_decls; 1688 1689 ClangASTContext::TemplateParameterInfos template_param_infos; 1690 TemplateParameterList *template_param_list = CreateTemplateParameterList( 1691 ast, template_param_infos, template_param_decls); 1692 1693 // LLDB needs to create those decls only to be able to display a 1694 // type that includes a template template argument. Only the name matters for 1695 // this purpose, so we use dummy values for the other characterisitcs of the 1696 // type. 1697 return TemplateTemplateParmDecl::Create( 1698 *ast, decl_ctx, SourceLocation(), 1699 /*Depth*/ 0, /*Position*/ 0, 1700 /*IsParameterPack*/ false, &identifier_info, template_param_list); 1701} 1702 1703ClassTemplateSpecializationDecl * 1704ClangASTContext::CreateClassTemplateSpecializationDecl( 1705 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind, 1706 const TemplateParameterInfos &template_param_infos) { 1707 ASTContext *ast = getASTContext(); 1708 llvm::SmallVector<clang::TemplateArgument, 2> args( 1709 template_param_infos.args.size() + 1710 (template_param_infos.packed_args ? 1 : 0)); 1711 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(), 1712 args.begin()); 1713 if (template_param_infos.packed_args) { 1714 args[args.size() - 1] = TemplateArgument::CreatePackCopy( 1715 *ast, template_param_infos.packed_args->args); 1716 } 1717 ClassTemplateSpecializationDecl *class_template_specialization_decl = 1718 ClassTemplateSpecializationDecl::Create( 1719 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(), 1720 SourceLocation(), class_template_decl, args, 1721 nullptr); 1722 1723 class_template_specialization_decl->setSpecializationKind( 1724 TSK_ExplicitSpecialization); 1725 1726 return class_template_specialization_decl; 1727} 1728 1729CompilerType ClangASTContext::CreateClassTemplateSpecializationType( 1730 ClassTemplateSpecializationDecl *class_template_specialization_decl) { 1731 if (class_template_specialization_decl) { 1732 ASTContext *ast = getASTContext(); 1733 if (ast) 1734 return CompilerType( 1735 ast, ast->getTagDeclType(class_template_specialization_decl)); 1736 } 1737 return CompilerType(); 1738} 1739 1740static inline bool check_op_param(bool is_method, 1741 clang::OverloadedOperatorKind op_kind, 1742 bool unary, bool binary, 1743 uint32_t num_params) { 1744 // Special-case call since it can take any number of operands 1745 if (op_kind == OO_Call) 1746 return true; 1747 1748 // The parameter count doesn't include "this" 1749 if (is_method) 1750 ++num_params; 1751 if (num_params == 1) 1752 return unary; 1753 if (num_params == 2) 1754 return binary; 1755 else 1756 return false; 1757} 1758 1759bool ClangASTContext::CheckOverloadedOperatorKindParameterCount( 1760 bool is_method, clang::OverloadedOperatorKind op_kind, 1761 uint32_t num_params) { 1762 switch (op_kind) { 1763 default: 1764 break; 1765 // C++ standard allows any number of arguments to new/delete 1766 case OO_New: 1767 case OO_Array_New: 1768 case OO_Delete: 1769 case OO_Array_Delete: 1770 return true; 1771 } 1772 1773#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ 1774 case OO_##Name: \ 1775 return check_op_param(is_method, op_kind, Unary, Binary, num_params); 1776 switch (op_kind) { 1777#include "clang/Basic/OperatorKinds.def" 1778 default: 1779 break; 1780 } 1781 return false; 1782} 1783 1784clang::AccessSpecifier 1785ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs, 1786 clang::AccessSpecifier rhs) { 1787 // Make the access equal to the stricter of the field and the nested field's 1788 // access 1789 if (lhs == AS_none || rhs == AS_none) 1790 return AS_none; 1791 if (lhs == AS_private || rhs == AS_private) 1792 return AS_private; 1793 if (lhs == AS_protected || rhs == AS_protected) 1794 return AS_protected; 1795 return AS_public; 1796} 1797 1798bool ClangASTContext::FieldIsBitfield(FieldDecl *field, 1799 uint32_t &bitfield_bit_size) { 1800 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size); 1801} 1802 1803bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field, 1804 uint32_t &bitfield_bit_size) { 1805 if (ast == nullptr || field == nullptr) 1806 return false; 1807 1808 if (field->isBitField()) { 1809 Expr *bit_width_expr = field->getBitWidth(); 1810 if (bit_width_expr) { 1811 llvm::APSInt bit_width_apsint; 1812 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) { 1813 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX); 1814 return true; 1815 } 1816 } 1817 } 1818 return false; 1819} 1820 1821bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) { 1822 if (record_decl == nullptr) 1823 return false; 1824 1825 if (!record_decl->field_empty()) 1826 return true; 1827 1828 // No fields, lets check this is a CXX record and check the base classes 1829 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1830 if (cxx_record_decl) { 1831 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1832 for (base_class = cxx_record_decl->bases_begin(), 1833 base_class_end = cxx_record_decl->bases_end(); 1834 base_class != base_class_end; ++base_class) { 1835 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>( 1836 base_class->getType()->getAs<RecordType>()->getDecl()); 1837 if (RecordHasFields(base_class_decl)) 1838 return true; 1839 } 1840 } 1841 return false; 1842} 1843 1844#pragma mark Objective-C Classes 1845 1846CompilerType ClangASTContext::CreateObjCClass(const char *name, 1847 DeclContext *decl_ctx, 1848 bool isForwardDecl, 1849 bool isInternal, 1850 ClangASTMetadata *metadata) { 1851 ASTContext *ast = getASTContext(); 1852 assert(ast != nullptr); 1853 assert(name && name[0]); 1854 if (decl_ctx == nullptr) 1855 decl_ctx = ast->getTranslationUnitDecl(); 1856 1857 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create( 1858 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr, 1859 nullptr, SourceLocation(), 1860 /*isForwardDecl,*/ 1861 isInternal); 1862 1863 if (decl && metadata) 1864 SetMetadata(ast, decl, *metadata); 1865 1866 return CompilerType(ast, ast->getObjCInterfaceType(decl)); 1867} 1868 1869static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) { 1870 return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()); 1871} 1872 1873uint32_t 1874ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl, 1875 bool omit_empty_base_classes) { 1876 uint32_t num_bases = 0; 1877 if (cxx_record_decl) { 1878 if (omit_empty_base_classes) { 1879 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1880 for (base_class = cxx_record_decl->bases_begin(), 1881 base_class_end = cxx_record_decl->bases_end(); 1882 base_class != base_class_end; ++base_class) { 1883 // Skip empty base classes 1884 if (omit_empty_base_classes) { 1885 if (BaseSpecifierIsEmpty(base_class)) 1886 continue; 1887 } 1888 ++num_bases; 1889 } 1890 } else 1891 num_bases = cxx_record_decl->getNumBases(); 1892 } 1893 return num_bases; 1894} 1895 1896#pragma mark Namespace Declarations 1897 1898NamespaceDecl * 1899ClangASTContext::GetUniqueNamespaceDeclaration(const char *name, 1900 DeclContext *decl_ctx) { 1901 NamespaceDecl *namespace_decl = nullptr; 1902 ASTContext *ast = getASTContext(); 1903 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl(); 1904 if (decl_ctx == nullptr) 1905 decl_ctx = translation_unit_decl; 1906 1907 if (name) { 1908 IdentifierInfo &identifier_info = ast->Idents.get(name); 1909 DeclarationName decl_name(&identifier_info); 1910 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); 1911 for (NamedDecl *decl : result) { 1912 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl); 1913 if (namespace_decl) 1914 return namespace_decl; 1915 } 1916 1917 namespace_decl = 1918 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(), 1919 SourceLocation(), &identifier_info, nullptr); 1920 1921 decl_ctx->addDecl(namespace_decl); 1922 } else { 1923 if (decl_ctx == translation_unit_decl) { 1924 namespace_decl = translation_unit_decl->getAnonymousNamespace(); 1925 if (namespace_decl) 1926 return namespace_decl; 1927 1928 namespace_decl = 1929 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(), 1930 SourceLocation(), nullptr, nullptr); 1931 translation_unit_decl->setAnonymousNamespace(namespace_decl); 1932 translation_unit_decl->addDecl(namespace_decl); 1933 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace()); 1934 } else { 1935 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx); 1936 if (parent_namespace_decl) { 1937 namespace_decl = parent_namespace_decl->getAnonymousNamespace(); 1938 if (namespace_decl) 1939 return namespace_decl; 1940 namespace_decl = 1941 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(), 1942 SourceLocation(), nullptr, nullptr); 1943 parent_namespace_decl->setAnonymousNamespace(namespace_decl); 1944 parent_namespace_decl->addDecl(namespace_decl); 1945 assert(namespace_decl == 1946 parent_namespace_decl->getAnonymousNamespace()); 1947 } else { 1948 // BAD!!! 1949 } 1950 } 1951 } 1952#ifdef LLDB_CONFIGURATION_DEBUG 1953 VerifyDecl(namespace_decl); 1954#endif 1955 return namespace_decl; 1956} 1957 1958NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration( 1959 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) { 1960 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast); 1961 if (ast_ctx == nullptr) 1962 return nullptr; 1963 1964 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx); 1965} 1966 1967clang::BlockDecl * 1968ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) { 1969 if (ctx != nullptr) { 1970 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx, 1971 clang::SourceLocation()); 1972 ctx->addDecl(decl); 1973 return decl; 1974 } 1975 return nullptr; 1976} 1977 1978clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left, 1979 clang::DeclContext *right, 1980 clang::DeclContext *root) { 1981 if (root == nullptr) 1982 return nullptr; 1983 1984 std::set<clang::DeclContext *> path_left; 1985 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent()) 1986 path_left.insert(d); 1987 1988 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent()) 1989 if (path_left.find(d) != path_left.end()) 1990 return d; 1991 1992 return nullptr; 1993} 1994 1995clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration( 1996 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) { 1997 if (decl_ctx != nullptr && ns_decl != nullptr) { 1998 clang::TranslationUnitDecl *translation_unit = 1999 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext()); 2000 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create( 2001 *getASTContext(), decl_ctx, clang::SourceLocation(), 2002 clang::SourceLocation(), clang::NestedNameSpecifierLoc(), 2003 clang::SourceLocation(), ns_decl, 2004 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit)); 2005 decl_ctx->addDecl(using_decl); 2006 return using_decl; 2007 } 2008 return nullptr; 2009} 2010 2011clang::UsingDecl * 2012ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx, 2013 clang::NamedDecl *target) { 2014 if (current_decl_ctx != nullptr && target != nullptr) { 2015 clang::UsingDecl *using_decl = clang::UsingDecl::Create( 2016 *getASTContext(), current_decl_ctx, clang::SourceLocation(), 2017 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false); 2018 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create( 2019 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl, 2020 target); 2021 using_decl->addShadowDecl(shadow_decl); 2022 current_decl_ctx->addDecl(using_decl); 2023 return using_decl; 2024 } 2025 return nullptr; 2026} 2027 2028clang::VarDecl *ClangASTContext::CreateVariableDeclaration( 2029 clang::DeclContext *decl_context, const char *name, clang::QualType type) { 2030 if (decl_context != nullptr) { 2031 clang::VarDecl *var_decl = clang::VarDecl::Create( 2032 *getASTContext(), decl_context, clang::SourceLocation(), 2033 clang::SourceLocation(), 2034 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type, 2035 nullptr, clang::SC_None); 2036 var_decl->setAccess(clang::AS_public); 2037 decl_context->addDecl(var_decl); 2038 return var_decl; 2039 } 2040 return nullptr; 2041} 2042 2043lldb::opaque_compiler_type_t 2044ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast, 2045 lldb::BasicType basic_type) { 2046 switch (basic_type) { 2047 case eBasicTypeVoid: 2048 return ast->VoidTy.getAsOpaquePtr(); 2049 case eBasicTypeChar: 2050 return ast->CharTy.getAsOpaquePtr(); 2051 case eBasicTypeSignedChar: 2052 return ast->SignedCharTy.getAsOpaquePtr(); 2053 case eBasicTypeUnsignedChar: 2054 return ast->UnsignedCharTy.getAsOpaquePtr(); 2055 case eBasicTypeWChar: 2056 return ast->getWCharType().getAsOpaquePtr(); 2057 case eBasicTypeSignedWChar: 2058 return ast->getSignedWCharType().getAsOpaquePtr(); 2059 case eBasicTypeUnsignedWChar: 2060 return ast->getUnsignedWCharType().getAsOpaquePtr(); 2061 case eBasicTypeChar16: 2062 return ast->Char16Ty.getAsOpaquePtr(); 2063 case eBasicTypeChar32: 2064 return ast->Char32Ty.getAsOpaquePtr(); 2065 case eBasicTypeShort: 2066 return ast->ShortTy.getAsOpaquePtr(); 2067 case eBasicTypeUnsignedShort: 2068 return ast->UnsignedShortTy.getAsOpaquePtr(); 2069 case eBasicTypeInt: 2070 return ast->IntTy.getAsOpaquePtr(); 2071 case eBasicTypeUnsignedInt: 2072 return ast->UnsignedIntTy.getAsOpaquePtr(); 2073 case eBasicTypeLong: 2074 return ast->LongTy.getAsOpaquePtr(); 2075 case eBasicTypeUnsignedLong: 2076 return ast->UnsignedLongTy.getAsOpaquePtr(); 2077 case eBasicTypeLongLong: 2078 return ast->LongLongTy.getAsOpaquePtr(); 2079 case eBasicTypeUnsignedLongLong: 2080 return ast->UnsignedLongLongTy.getAsOpaquePtr(); 2081 case eBasicTypeInt128: 2082 return ast->Int128Ty.getAsOpaquePtr(); 2083 case eBasicTypeUnsignedInt128: 2084 return ast->UnsignedInt128Ty.getAsOpaquePtr(); 2085 case eBasicTypeBool: 2086 return ast->BoolTy.getAsOpaquePtr(); 2087 case eBasicTypeHalf: 2088 return ast->HalfTy.getAsOpaquePtr(); 2089 case eBasicTypeFloat: 2090 return ast->FloatTy.getAsOpaquePtr(); 2091 case eBasicTypeDouble: 2092 return ast->DoubleTy.getAsOpaquePtr(); 2093 case eBasicTypeLongDouble: 2094 return ast->LongDoubleTy.getAsOpaquePtr(); 2095 case eBasicTypeFloatComplex: 2096 return ast->FloatComplexTy.getAsOpaquePtr(); 2097 case eBasicTypeDoubleComplex: 2098 return ast->DoubleComplexTy.getAsOpaquePtr(); 2099 case eBasicTypeLongDoubleComplex: 2100 return ast->LongDoubleComplexTy.getAsOpaquePtr(); 2101 case eBasicTypeObjCID: 2102 return ast->getObjCIdType().getAsOpaquePtr(); 2103 case eBasicTypeObjCClass: 2104 return ast->getObjCClassType().getAsOpaquePtr(); 2105 case eBasicTypeObjCSel: 2106 return ast->getObjCSelType().getAsOpaquePtr(); 2107 case eBasicTypeNullPtr: 2108 return ast->NullPtrTy.getAsOpaquePtr(); 2109 default: 2110 return nullptr; 2111 } 2112} 2113 2114#pragma mark Function Types 2115 2116clang::DeclarationName 2117ClangASTContext::GetDeclarationName(const char *name, 2118 const CompilerType &function_clang_type) { 2119 if (!name || !name[0]) 2120 return clang::DeclarationName(); 2121 2122 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS; 2123 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS) 2124 return DeclarationName(&getASTContext()->Idents.get( 2125 name)); // Not operator, but a regular function. 2126 2127 // Check the number of operator parameters. Sometimes we have seen bad DWARF 2128 // that doesn't correctly describe operators and if we try to create a method 2129 // and add it to the class, clang will assert and crash, so we need to make 2130 // sure things are acceptable. 2131 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type)); 2132 const clang::FunctionProtoType *function_type = 2133 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr()); 2134 if (function_type == nullptr) 2135 return clang::DeclarationName(); 2136 2137 const bool is_method = false; 2138 const unsigned int num_params = function_type->getNumParams(); 2139 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount( 2140 is_method, op_kind, num_params)) 2141 return clang::DeclarationName(); 2142 2143 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind); 2144} 2145 2146FunctionDecl *ClangASTContext::CreateFunctionDeclaration( 2147 DeclContext *decl_ctx, const char *name, 2148 const CompilerType &function_clang_type, int storage, bool is_inline) { 2149 FunctionDecl *func_decl = nullptr; 2150 ASTContext *ast = getASTContext(); 2151 if (decl_ctx == nullptr) 2152 decl_ctx = ast->getTranslationUnitDecl(); 2153 2154 const bool hasWrittenPrototype = true; 2155 const bool isConstexprSpecified = false; 2156 2157 clang::DeclarationName declarationName = 2158 GetDeclarationName(name, function_clang_type); 2159 func_decl = FunctionDecl::Create( 2160 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName, 2161 ClangUtil::GetQualType(function_clang_type), nullptr, 2162 (clang::StorageClass)storage, is_inline, hasWrittenPrototype, 2163 isConstexprSpecified); 2164 if (func_decl) 2165 decl_ctx->addDecl(func_decl); 2166 2167#ifdef LLDB_CONFIGURATION_DEBUG 2168 VerifyDecl(func_decl); 2169#endif 2170 2171 return func_decl; 2172} 2173 2174CompilerType ClangASTContext::CreateFunctionType( 2175 ASTContext *ast, const CompilerType &result_type, const CompilerType *args, 2176 unsigned num_args, bool is_variadic, unsigned type_quals, 2177 clang::CallingConv cc) { 2178 if (ast == nullptr) 2179 return CompilerType(); // invalid AST 2180 2181 if (!result_type || !ClangUtil::IsClangType(result_type)) 2182 return CompilerType(); // invalid return type 2183 2184 std::vector<QualType> qual_type_args; 2185 if (num_args > 0 && args == nullptr) 2186 return CompilerType(); // invalid argument array passed in 2187 2188 // Verify that all arguments are valid and the right type 2189 for (unsigned i = 0; i < num_args; ++i) { 2190 if (args[i]) { 2191 // Make sure we have a clang type in args[i] and not a type from another 2192 // language whose name might match 2193 const bool is_clang_type = ClangUtil::IsClangType(args[i]); 2194 lldbassert(is_clang_type); 2195 if (is_clang_type) 2196 qual_type_args.push_back(ClangUtil::GetQualType(args[i])); 2197 else 2198 return CompilerType(); // invalid argument type (must be a clang type) 2199 } else 2200 return CompilerType(); // invalid argument type (empty) 2201 } 2202 2203 // TODO: Detect calling convention in DWARF? 2204 FunctionProtoType::ExtProtoInfo proto_info; 2205 proto_info.ExtInfo = cc; 2206 proto_info.Variadic = is_variadic; 2207 proto_info.ExceptionSpec = EST_None; 2208 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals); 2209 proto_info.RefQualifier = RQ_None; 2210 2211 return CompilerType(ast, 2212 ast->getFunctionType(ClangUtil::GetQualType(result_type), 2213 qual_type_args, proto_info)); 2214} 2215 2216ParmVarDecl *ClangASTContext::CreateParameterDeclaration( 2217 clang::DeclContext *decl_ctx, const char *name, 2218 const CompilerType ¶m_type, int storage) { 2219 ASTContext *ast = getASTContext(); 2220 assert(ast != nullptr); 2221 auto *decl = 2222 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), 2223 name && name[0] ? &ast->Idents.get(name) : nullptr, 2224 ClangUtil::GetQualType(param_type), nullptr, 2225 (clang::StorageClass)storage, nullptr); 2226 decl_ctx->addDecl(decl); 2227 return decl; 2228} 2229 2230void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl, 2231 ParmVarDecl **params, 2232 unsigned num_params) { 2233 if (function_decl) 2234 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params)); 2235} 2236 2237CompilerType 2238ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) { 2239 QualType block_type = m_ast_ap->getBlockPointerType( 2240 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType())); 2241 2242 return CompilerType(this, block_type.getAsOpaquePtr()); 2243} 2244 2245#pragma mark Array Types 2246 2247CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type, 2248 size_t element_count, 2249 bool is_vector) { 2250 if (element_type.IsValid()) { 2251 ASTContext *ast = getASTContext(); 2252 assert(ast != nullptr); 2253 2254 if (is_vector) { 2255 return CompilerType( 2256 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type), 2257 element_count)); 2258 } else { 2259 2260 llvm::APInt ap_element_count(64, element_count); 2261 if (element_count == 0) { 2262 return CompilerType(ast, ast->getIncompleteArrayType( 2263 ClangUtil::GetQualType(element_type), 2264 clang::ArrayType::Normal, 0)); 2265 } else { 2266 return CompilerType( 2267 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type), 2268 ap_element_count, 2269 clang::ArrayType::Normal, 0)); 2270 } 2271 } 2272 } 2273 return CompilerType(); 2274} 2275 2276CompilerType ClangASTContext::CreateStructForIdentifier( 2277 const ConstString &type_name, 2278 const std::initializer_list<std::pair<const char *, CompilerType>> 2279 &type_fields, 2280 bool packed) { 2281 CompilerType type; 2282 if (!type_name.IsEmpty() && 2283 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)) 2284 .IsValid()) { 2285 lldbassert(0 && "Trying to create a type for an existing name"); 2286 return type; 2287 } 2288 2289 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), 2290 clang::TTK_Struct, lldb::eLanguageTypeC); 2291 StartTagDeclarationDefinition(type); 2292 for (const auto &field : type_fields) 2293 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, 2294 0); 2295 if (packed) 2296 SetIsPacked(type); 2297 CompleteTagDeclarationDefinition(type); 2298 return type; 2299} 2300 2301CompilerType ClangASTContext::GetOrCreateStructForIdentifier( 2302 const ConstString &type_name, 2303 const std::initializer_list<std::pair<const char *, CompilerType>> 2304 &type_fields, 2305 bool packed) { 2306 CompilerType type; 2307 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid()) 2308 return type; 2309 2310 return CreateStructForIdentifier(type_name, type_fields, packed); 2311} 2312 2313#pragma mark Enumeration Types 2314 2315CompilerType 2316ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx, 2317 const Declaration &decl, 2318 const CompilerType &integer_clang_type, 2319 bool is_scoped) { 2320 // TODO: Do something intelligent with the Declaration object passed in 2321 // like maybe filling in the SourceLocation with it... 2322 ASTContext *ast = getASTContext(); 2323 2324 // TODO: ask about these... 2325 // const bool IsFixed = false; 2326 2327 EnumDecl *enum_decl = EnumDecl::Create( 2328 *ast, decl_ctx, SourceLocation(), SourceLocation(), 2329 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr, 2330 is_scoped, // IsScoped 2331 is_scoped, // IsScopedUsingClassTag 2332 false); // IsFixed 2333 2334 if (enum_decl) { 2335 if (decl_ctx) 2336 decl_ctx->addDecl(enum_decl); 2337 2338 // TODO: check if we should be setting the promotion type too? 2339 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type)); 2340 2341 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info 2342 2343 return CompilerType(ast, ast->getTagDeclType(enum_decl)); 2344 } 2345 return CompilerType(); 2346} 2347 2348CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast, 2349 size_t bit_size, 2350 bool is_signed) { 2351 if (ast) { 2352 if (is_signed) { 2353 if (bit_size == ast->getTypeSize(ast->SignedCharTy)) 2354 return CompilerType(ast, ast->SignedCharTy); 2355 2356 if (bit_size == ast->getTypeSize(ast->ShortTy)) 2357 return CompilerType(ast, ast->ShortTy); 2358 2359 if (bit_size == ast->getTypeSize(ast->IntTy)) 2360 return CompilerType(ast, ast->IntTy); 2361 2362 if (bit_size == ast->getTypeSize(ast->LongTy)) 2363 return CompilerType(ast, ast->LongTy); 2364 2365 if (bit_size == ast->getTypeSize(ast->LongLongTy)) 2366 return CompilerType(ast, ast->LongLongTy); 2367 2368 if (bit_size == ast->getTypeSize(ast->Int128Ty)) 2369 return CompilerType(ast, ast->Int128Ty); 2370 } else { 2371 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy)) 2372 return CompilerType(ast, ast->UnsignedCharTy); 2373 2374 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy)) 2375 return CompilerType(ast, ast->UnsignedShortTy); 2376 2377 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy)) 2378 return CompilerType(ast, ast->UnsignedIntTy); 2379 2380 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy)) 2381 return CompilerType(ast, ast->UnsignedLongTy); 2382 2383 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy)) 2384 return CompilerType(ast, ast->UnsignedLongLongTy); 2385 2386 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty)) 2387 return CompilerType(ast, ast->UnsignedInt128Ty); 2388 } 2389 } 2390 return CompilerType(); 2391} 2392 2393CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast, 2394 bool is_signed) { 2395 if (ast) 2396 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), 2397 is_signed); 2398 return CompilerType(); 2399} 2400 2401void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) { 2402 if (decl_ctx) { 2403 DumpDeclContextHiearchy(decl_ctx->getParent()); 2404 2405 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx); 2406 if (named_decl) { 2407 printf("%20s: %s\n", decl_ctx->getDeclKindName(), 2408 named_decl->getDeclName().getAsString().c_str()); 2409 } else { 2410 printf("%20s\n", decl_ctx->getDeclKindName()); 2411 } 2412 } 2413} 2414 2415void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) { 2416 if (decl == nullptr) 2417 return; 2418 DumpDeclContextHiearchy(decl->getDeclContext()); 2419 2420 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl); 2421 if (record_decl) { 2422 printf("%20s: %s%s\n", decl->getDeclKindName(), 2423 record_decl->getDeclName().getAsString().c_str(), 2424 record_decl->isInjectedClassName() ? " (injected class name)" : ""); 2425 2426 } else { 2427 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl); 2428 if (named_decl) { 2429 printf("%20s: %s\n", decl->getDeclKindName(), 2430 named_decl->getDeclName().getAsString().c_str()); 2431 } else { 2432 printf("%20s\n", decl->getDeclKindName()); 2433 } 2434 } 2435} 2436 2437bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl, 2438 clang::Decl *rhs_decl) { 2439 if (lhs_decl && rhs_decl) { 2440 //---------------------------------------------------------------------- 2441 // Make sure the decl kinds match first 2442 //---------------------------------------------------------------------- 2443 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind(); 2444 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind(); 2445 2446 if (lhs_decl_kind == rhs_decl_kind) { 2447 //------------------------------------------------------------------ 2448 // Now check that the decl contexts kinds are all equivalent before we 2449 // have to check any names of the decl contexts... 2450 //------------------------------------------------------------------ 2451 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext(); 2452 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext(); 2453 if (lhs_decl_ctx && rhs_decl_ctx) { 2454 while (1) { 2455 if (lhs_decl_ctx && rhs_decl_ctx) { 2456 const clang::Decl::Kind lhs_decl_ctx_kind = 2457 lhs_decl_ctx->getDeclKind(); 2458 const clang::Decl::Kind rhs_decl_ctx_kind = 2459 rhs_decl_ctx->getDeclKind(); 2460 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) { 2461 lhs_decl_ctx = lhs_decl_ctx->getParent(); 2462 rhs_decl_ctx = rhs_decl_ctx->getParent(); 2463 2464 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr) 2465 break; 2466 } else 2467 return false; 2468 } else 2469 return false; 2470 } 2471 2472 //-------------------------------------------------------------- 2473 // Now make sure the name of the decls match 2474 //-------------------------------------------------------------- 2475 clang::NamedDecl *lhs_named_decl = 2476 llvm::dyn_cast<clang::NamedDecl>(lhs_decl); 2477 clang::NamedDecl *rhs_named_decl = 2478 llvm::dyn_cast<clang::NamedDecl>(rhs_decl); 2479 if (lhs_named_decl && rhs_named_decl) { 2480 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName(); 2481 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName(); 2482 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) { 2483 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString()) 2484 return false; 2485 } else 2486 return false; 2487 } else 2488 return false; 2489 2490 //-------------------------------------------------------------- 2491 // We know that the decl context kinds all match, so now we need to 2492 // make sure the names match as well 2493 //-------------------------------------------------------------- 2494 lhs_decl_ctx = lhs_decl->getDeclContext(); 2495 rhs_decl_ctx = rhs_decl->getDeclContext(); 2496 while (1) { 2497 switch (lhs_decl_ctx->getDeclKind()) { 2498 case clang::Decl::TranslationUnit: 2499 // We don't care about the translation unit names 2500 return true; 2501 default: { 2502 clang::NamedDecl *lhs_named_decl = 2503 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx); 2504 clang::NamedDecl *rhs_named_decl = 2505 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx); 2506 if (lhs_named_decl && rhs_named_decl) { 2507 clang::DeclarationName lhs_decl_name = 2508 lhs_named_decl->getDeclName(); 2509 clang::DeclarationName rhs_decl_name = 2510 rhs_named_decl->getDeclName(); 2511 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) { 2512 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString()) 2513 return false; 2514 } else 2515 return false; 2516 } else 2517 return false; 2518 } break; 2519 } 2520 lhs_decl_ctx = lhs_decl_ctx->getParent(); 2521 rhs_decl_ctx = rhs_decl_ctx->getParent(); 2522 } 2523 } 2524 } 2525 } 2526 return false; 2527} 2528bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast, 2529 clang::Decl *decl) { 2530 if (!decl) 2531 return false; 2532 2533 ExternalASTSource *ast_source = ast->getExternalSource(); 2534 2535 if (!ast_source) 2536 return false; 2537 2538 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) { 2539 if (tag_decl->isCompleteDefinition()) 2540 return true; 2541 2542 if (!tag_decl->hasExternalLexicalStorage()) 2543 return false; 2544 2545 ast_source->CompleteType(tag_decl); 2546 2547 return !tag_decl->getTypeForDecl()->isIncompleteType(); 2548 } else if (clang::ObjCInterfaceDecl *objc_interface_decl = 2549 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) { 2550 if (objc_interface_decl->getDefinition()) 2551 return true; 2552 2553 if (!objc_interface_decl->hasExternalLexicalStorage()) 2554 return false; 2555 2556 ast_source->CompleteType(objc_interface_decl); 2557 2558 return !objc_interface_decl->getTypeForDecl()->isIncompleteType(); 2559 } else { 2560 return false; 2561 } 2562} 2563 2564void ClangASTContext::SetMetadataAsUserID(const void *object, 2565 user_id_t user_id) { 2566 ClangASTMetadata meta_data; 2567 meta_data.SetUserID(user_id); 2568 SetMetadata(object, meta_data); 2569} 2570 2571void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object, 2572 ClangASTMetadata &metadata) { 2573 ClangExternalASTSourceCommon *external_source = 2574 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource()); 2575 2576 if (external_source) 2577 external_source->SetMetadata(object, metadata); 2578} 2579 2580ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast, 2581 const void *object) { 2582 ClangExternalASTSourceCommon *external_source = 2583 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource()); 2584 2585 if (external_source && external_source->HasMetadata(object)) 2586 return external_source->GetMetadata(object); 2587 else 2588 return nullptr; 2589} 2590 2591clang::DeclContext * 2592ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) { 2593 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl); 2594} 2595 2596clang::DeclContext * 2597ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) { 2598 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl); 2599} 2600 2601bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type, 2602 int kind) const { 2603 const clang::Type *clang_type = tag_qual_type.getTypePtr(); 2604 if (clang_type) { 2605 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type); 2606 if (tag_type) { 2607 clang::TagDecl *tag_decl = 2608 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl()); 2609 if (tag_decl) { 2610 tag_decl->setTagKind((clang::TagDecl::TagKind)kind); 2611 return true; 2612 } 2613 } 2614 } 2615 return false; 2616} 2617 2618bool ClangASTContext::SetDefaultAccessForRecordFields( 2619 clang::RecordDecl *record_decl, int default_accessibility, 2620 int *assigned_accessibilities, size_t num_assigned_accessibilities) { 2621 if (record_decl) { 2622 uint32_t field_idx; 2623 clang::RecordDecl::field_iterator field, field_end; 2624 for (field = record_decl->field_begin(), 2625 field_end = record_decl->field_end(), field_idx = 0; 2626 field != field_end; ++field, ++field_idx) { 2627 // If no accessibility was assigned, assign the correct one 2628 if (field_idx < num_assigned_accessibilities && 2629 assigned_accessibilities[field_idx] == clang::AS_none) 2630 field->setAccess((clang::AccessSpecifier)default_accessibility); 2631 } 2632 return true; 2633 } 2634 return false; 2635} 2636 2637clang::DeclContext * 2638ClangASTContext::GetDeclContextForType(const CompilerType &type) { 2639 return GetDeclContextForType(ClangUtil::GetQualType(type)); 2640} 2641 2642clang::DeclContext * 2643ClangASTContext::GetDeclContextForType(clang::QualType type) { 2644 if (type.isNull()) 2645 return nullptr; 2646 2647 clang::QualType qual_type = type.getCanonicalType(); 2648 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2649 switch (type_class) { 2650 case clang::Type::ObjCInterface: 2651 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr()) 2652 ->getInterface(); 2653 case clang::Type::ObjCObjectPointer: 2654 return GetDeclContextForType( 2655 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()) 2656 ->getPointeeType()); 2657 case clang::Type::Record: 2658 return llvm::cast<clang::RecordType>(qual_type)->getDecl(); 2659 case clang::Type::Enum: 2660 return llvm::cast<clang::EnumType>(qual_type)->getDecl(); 2661 case clang::Type::Typedef: 2662 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type) 2663 ->getDecl() 2664 ->getUnderlyingType()); 2665 case clang::Type::Auto: 2666 return GetDeclContextForType( 2667 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()); 2668 case clang::Type::Elaborated: 2669 return GetDeclContextForType( 2670 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()); 2671 case clang::Type::Paren: 2672 return GetDeclContextForType( 2673 llvm::cast<clang::ParenType>(qual_type)->desugar()); 2674 default: 2675 break; 2676 } 2677 // No DeclContext in this type... 2678 return nullptr; 2679} 2680 2681static bool GetCompleteQualType(clang::ASTContext *ast, 2682 clang::QualType qual_type, 2683 bool allow_completion = true) { 2684 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2685 switch (type_class) { 2686 case clang::Type::ConstantArray: 2687 case clang::Type::IncompleteArray: 2688 case clang::Type::VariableArray: { 2689 const clang::ArrayType *array_type = 2690 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr()); 2691 2692 if (array_type) 2693 return GetCompleteQualType(ast, array_type->getElementType(), 2694 allow_completion); 2695 } break; 2696 case clang::Type::Record: { 2697 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 2698 if (cxx_record_decl) { 2699 if (cxx_record_decl->hasExternalLexicalStorage()) { 2700 const bool is_complete = cxx_record_decl->isCompleteDefinition(); 2701 const bool fields_loaded = 2702 cxx_record_decl->hasLoadedFieldsFromExternalStorage(); 2703 if (is_complete && fields_loaded) 2704 return true; 2705 2706 if (!allow_completion) 2707 return false; 2708 2709 // Call the field_begin() accessor to for it to use the external source 2710 // to load the fields... 2711 clang::ExternalASTSource *external_ast_source = 2712 ast->getExternalSource(); 2713 if (external_ast_source) { 2714 external_ast_source->CompleteType(cxx_record_decl); 2715 if (cxx_record_decl->isCompleteDefinition()) { 2716 cxx_record_decl->field_begin(); 2717 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true); 2718 } 2719 } 2720 } 2721 } 2722 const clang::TagType *tag_type = 2723 llvm::cast<clang::TagType>(qual_type.getTypePtr()); 2724 return !tag_type->isIncompleteType(); 2725 } break; 2726 2727 case clang::Type::Enum: { 2728 const clang::TagType *tag_type = 2729 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()); 2730 if (tag_type) { 2731 clang::TagDecl *tag_decl = tag_type->getDecl(); 2732 if (tag_decl) { 2733 if (tag_decl->getDefinition()) 2734 return true; 2735 2736 if (!allow_completion) 2737 return false; 2738 2739 if (tag_decl->hasExternalLexicalStorage()) { 2740 if (ast) { 2741 clang::ExternalASTSource *external_ast_source = 2742 ast->getExternalSource(); 2743 if (external_ast_source) { 2744 external_ast_source->CompleteType(tag_decl); 2745 return !tag_type->isIncompleteType(); 2746 } 2747 } 2748 } 2749 return false; 2750 } 2751 } 2752 2753 } break; 2754 case clang::Type::ObjCObject: 2755 case clang::Type::ObjCInterface: { 2756 const clang::ObjCObjectType *objc_class_type = 2757 llvm::dyn_cast<clang::ObjCObjectType>(qual_type); 2758 if (objc_class_type) { 2759 clang::ObjCInterfaceDecl *class_interface_decl = 2760 objc_class_type->getInterface(); 2761 // We currently can't complete objective C types through the newly added 2762 // ASTContext because it only supports TagDecl objects right now... 2763 if (class_interface_decl) { 2764 if (class_interface_decl->getDefinition()) 2765 return true; 2766 2767 if (!allow_completion) 2768 return false; 2769 2770 if (class_interface_decl->hasExternalLexicalStorage()) { 2771 if (ast) { 2772 clang::ExternalASTSource *external_ast_source = 2773 ast->getExternalSource(); 2774 if (external_ast_source) { 2775 external_ast_source->CompleteType(class_interface_decl); 2776 return !objc_class_type->isIncompleteType(); 2777 } 2778 } 2779 } 2780 return false; 2781 } 2782 } 2783 } break; 2784 2785 case clang::Type::Typedef: 2786 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type) 2787 ->getDecl() 2788 ->getUnderlyingType(), 2789 allow_completion); 2790 2791 case clang::Type::Auto: 2792 return GetCompleteQualType( 2793 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(), 2794 allow_completion); 2795 2796 case clang::Type::Elaborated: 2797 return GetCompleteQualType( 2798 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), 2799 allow_completion); 2800 2801 case clang::Type::Paren: 2802 return GetCompleteQualType( 2803 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), 2804 allow_completion); 2805 2806 case clang::Type::Attributed: 2807 return GetCompleteQualType( 2808 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(), 2809 allow_completion); 2810 2811 default: 2812 break; 2813 } 2814 2815 return true; 2816} 2817 2818static clang::ObjCIvarDecl::AccessControl 2819ConvertAccessTypeToObjCIvarAccessControl(AccessType access) { 2820 switch (access) { 2821 case eAccessNone: 2822 return clang::ObjCIvarDecl::None; 2823 case eAccessPublic: 2824 return clang::ObjCIvarDecl::Public; 2825 case eAccessPrivate: 2826 return clang::ObjCIvarDecl::Private; 2827 case eAccessProtected: 2828 return clang::ObjCIvarDecl::Protected; 2829 case eAccessPackage: 2830 return clang::ObjCIvarDecl::Package; 2831 } 2832 return clang::ObjCIvarDecl::None; 2833} 2834 2835//---------------------------------------------------------------------- 2836// Tests 2837//---------------------------------------------------------------------- 2838 2839bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) { 2840 clang::QualType qual_type(GetCanonicalQualType(type)); 2841 2842 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2843 switch (type_class) { 2844 case clang::Type::IncompleteArray: 2845 case clang::Type::VariableArray: 2846 case clang::Type::ConstantArray: 2847 case clang::Type::ExtVector: 2848 case clang::Type::Vector: 2849 case clang::Type::Record: 2850 case clang::Type::ObjCObject: 2851 case clang::Type::ObjCInterface: 2852 return true; 2853 case clang::Type::Auto: 2854 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type) 2855 ->getDeducedType() 2856 .getAsOpaquePtr()); 2857 case clang::Type::Elaborated: 2858 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type) 2859 ->getNamedType() 2860 .getAsOpaquePtr()); 2861 case clang::Type::Typedef: 2862 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type) 2863 ->getDecl() 2864 ->getUnderlyingType() 2865 .getAsOpaquePtr()); 2866 case clang::Type::Paren: 2867 return IsAggregateType( 2868 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); 2869 default: 2870 break; 2871 } 2872 // The clang type does have a value 2873 return false; 2874} 2875 2876bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) { 2877 clang::QualType qual_type(GetCanonicalQualType(type)); 2878 2879 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2880 switch (type_class) { 2881 case clang::Type::Record: { 2882 if (const clang::RecordType *record_type = 2883 llvm::dyn_cast_or_null<clang::RecordType>( 2884 qual_type.getTypePtrOrNull())) { 2885 if (const clang::RecordDecl *record_decl = record_type->getDecl()) { 2886 return record_decl->isAnonymousStructOrUnion(); 2887 } 2888 } 2889 break; 2890 } 2891 case clang::Type::Auto: 2892 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type) 2893 ->getDeducedType() 2894 .getAsOpaquePtr()); 2895 case clang::Type::Elaborated: 2896 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type) 2897 ->getNamedType() 2898 .getAsOpaquePtr()); 2899 case clang::Type::Typedef: 2900 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type) 2901 ->getDecl() 2902 ->getUnderlyingType() 2903 .getAsOpaquePtr()); 2904 case clang::Type::Paren: 2905 return IsAnonymousType( 2906 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); 2907 default: 2908 break; 2909 } 2910 // The clang type does have a value 2911 return false; 2912} 2913 2914bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type, 2915 CompilerType *element_type_ptr, 2916 uint64_t *size, bool *is_incomplete) { 2917 clang::QualType qual_type(GetCanonicalQualType(type)); 2918 2919 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2920 switch (type_class) { 2921 default: 2922 break; 2923 2924 case clang::Type::ConstantArray: 2925 if (element_type_ptr) 2926 element_type_ptr->SetCompilerType( 2927 getASTContext(), 2928 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType()); 2929 if (size) 2930 *size = llvm::cast<clang::ConstantArrayType>(qual_type) 2931 ->getSize() 2932 .getLimitedValue(ULLONG_MAX); 2933 if (is_incomplete) 2934 *is_incomplete = false; 2935 return true; 2936 2937 case clang::Type::IncompleteArray: 2938 if (element_type_ptr) 2939 element_type_ptr->SetCompilerType( 2940 getASTContext(), 2941 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType()); 2942 if (size) 2943 *size = 0; 2944 if (is_incomplete) 2945 *is_incomplete = true; 2946 return true; 2947 2948 case clang::Type::VariableArray: 2949 if (element_type_ptr) 2950 element_type_ptr->SetCompilerType( 2951 getASTContext(), 2952 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType()); 2953 if (size) 2954 *size = 0; 2955 if (is_incomplete) 2956 *is_incomplete = false; 2957 return true; 2958 2959 case clang::Type::DependentSizedArray: 2960 if (element_type_ptr) 2961 element_type_ptr->SetCompilerType( 2962 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type) 2963 ->getElementType()); 2964 if (size) 2965 *size = 0; 2966 if (is_incomplete) 2967 *is_incomplete = false; 2968 return true; 2969 2970 case clang::Type::Typedef: 2971 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type) 2972 ->getDecl() 2973 ->getUnderlyingType() 2974 .getAsOpaquePtr(), 2975 element_type_ptr, size, is_incomplete); 2976 case clang::Type::Auto: 2977 return IsArrayType(llvm::cast<clang::AutoType>(qual_type) 2978 ->getDeducedType() 2979 .getAsOpaquePtr(), 2980 element_type_ptr, size, is_incomplete); 2981 case clang::Type::Elaborated: 2982 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type) 2983 ->getNamedType() 2984 .getAsOpaquePtr(), 2985 element_type_ptr, size, is_incomplete); 2986 case clang::Type::Paren: 2987 return IsArrayType( 2988 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), 2989 element_type_ptr, size, is_incomplete); 2990 } 2991 if (element_type_ptr) 2992 element_type_ptr->Clear(); 2993 if (size) 2994 *size = 0; 2995 if (is_incomplete) 2996 *is_incomplete = false; 2997 return false; 2998} 2999 3000bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type, 3001 CompilerType *element_type, uint64_t *size) { 3002 clang::QualType qual_type(GetCanonicalQualType(type)); 3003 3004 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3005 switch (type_class) { 3006 case clang::Type::Vector: { 3007 const clang::VectorType *vector_type = 3008 qual_type->getAs<clang::VectorType>(); 3009 if (vector_type) { 3010 if (size) 3011 *size = vector_type->getNumElements(); 3012 if (element_type) 3013 *element_type = 3014 CompilerType(getASTContext(), vector_type->getElementType()); 3015 } 3016 return true; 3017 } break; 3018 case clang::Type::ExtVector: { 3019 const clang::ExtVectorType *ext_vector_type = 3020 qual_type->getAs<clang::ExtVectorType>(); 3021 if (ext_vector_type) { 3022 if (size) 3023 *size = ext_vector_type->getNumElements(); 3024 if (element_type) 3025 *element_type = 3026 CompilerType(getASTContext(), ext_vector_type->getElementType()); 3027 } 3028 return true; 3029 } 3030 default: 3031 break; 3032 } 3033 return false; 3034} 3035 3036bool ClangASTContext::IsRuntimeGeneratedType( 3037 lldb::opaque_compiler_type_t type) { 3038 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext()) 3039 ->GetDeclContextForType(GetQualType(type)); 3040 if (!decl_ctx) 3041 return false; 3042 3043 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx)) 3044 return false; 3045 3046 clang::ObjCInterfaceDecl *result_iface_decl = 3047 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx); 3048 3049 ClangASTMetadata *ast_metadata = 3050 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl); 3051 if (!ast_metadata) 3052 return false; 3053 return (ast_metadata->GetISAPtr() != 0); 3054} 3055 3056bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) { 3057 return GetQualType(type).getUnqualifiedType()->isCharType(); 3058} 3059 3060bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) { 3061 const bool allow_completion = false; 3062 return GetCompleteQualType(getASTContext(), GetQualType(type), 3063 allow_completion); 3064} 3065 3066bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) { 3067 return GetQualType(type).isConstQualified(); 3068} 3069 3070bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type, 3071 uint32_t &length) { 3072 CompilerType pointee_or_element_clang_type; 3073 length = 0; 3074 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type)); 3075 3076 if (!pointee_or_element_clang_type.IsValid()) 3077 return false; 3078 3079 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) { 3080 if (pointee_or_element_clang_type.IsCharType()) { 3081 if (type_flags.Test(eTypeIsArray)) { 3082 // We know the size of the array and it could be a C string since it is 3083 // an array of characters 3084 length = llvm::cast<clang::ConstantArrayType>( 3085 GetCanonicalQualType(type).getTypePtr()) 3086 ->getSize() 3087 .getLimitedValue(); 3088 } 3089 return true; 3090 } 3091 } 3092 return false; 3093} 3094 3095bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type, 3096 bool *is_variadic_ptr) { 3097 if (type) { 3098 clang::QualType qual_type(GetCanonicalQualType(type)); 3099 3100 if (qual_type->isFunctionType()) { 3101 if (is_variadic_ptr) { 3102 const clang::FunctionProtoType *function_proto_type = 3103 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); 3104 if (function_proto_type) 3105 *is_variadic_ptr = function_proto_type->isVariadic(); 3106 else 3107 *is_variadic_ptr = false; 3108 } 3109 return true; 3110 } 3111 3112 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3113 switch (type_class) { 3114 default: 3115 break; 3116 case clang::Type::Typedef: 3117 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type) 3118 ->getDecl() 3119 ->getUnderlyingType() 3120 .getAsOpaquePtr(), 3121 nullptr); 3122 case clang::Type::Auto: 3123 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type) 3124 ->getDeducedType() 3125 .getAsOpaquePtr(), 3126 nullptr); 3127 case clang::Type::Elaborated: 3128 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type) 3129 ->getNamedType() 3130 .getAsOpaquePtr(), 3131 nullptr); 3132 case clang::Type::Paren: 3133 return IsFunctionType( 3134 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), 3135 nullptr); 3136 case clang::Type::LValueReference: 3137 case clang::Type::RValueReference: { 3138 const clang::ReferenceType *reference_type = 3139 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); 3140 if (reference_type) 3141 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), 3142 nullptr); 3143 } break; 3144 } 3145 } 3146 return false; 3147} 3148 3149// Used to detect "Homogeneous Floating-point Aggregates" 3150uint32_t 3151ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, 3152 CompilerType *base_type_ptr) { 3153 if (!type) 3154 return 0; 3155 3156 clang::QualType qual_type(GetCanonicalQualType(type)); 3157 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3158 switch (type_class) { 3159 case clang::Type::Record: 3160 if (GetCompleteType(type)) { 3161 const clang::CXXRecordDecl *cxx_record_decl = 3162 qual_type->getAsCXXRecordDecl(); 3163 if (cxx_record_decl) { 3164 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass()) 3165 return 0; 3166 } 3167 const clang::RecordType *record_type = 3168 llvm::cast<clang::RecordType>(qual_type.getTypePtr()); 3169 if (record_type) { 3170 const clang::RecordDecl *record_decl = record_type->getDecl(); 3171 if (record_decl) { 3172 // We are looking for a structure that contains only floating point 3173 // types 3174 clang::RecordDecl::field_iterator field_pos, 3175 field_end = record_decl->field_end(); 3176 uint32_t num_fields = 0; 3177 bool is_hva = false; 3178 bool is_hfa = false; 3179 clang::QualType base_qual_type; 3180 uint64_t base_bitwidth = 0; 3181 for (field_pos = record_decl->field_begin(); field_pos != field_end; 3182 ++field_pos) { 3183 clang::QualType field_qual_type = field_pos->getType(); 3184 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type); 3185 if (field_qual_type->isFloatingType()) { 3186 if (field_qual_type->isComplexType()) 3187 return 0; 3188 else { 3189 if (num_fields == 0) 3190 base_qual_type = field_qual_type; 3191 else { 3192 if (is_hva) 3193 return 0; 3194 is_hfa = true; 3195 if (field_qual_type.getTypePtr() != 3196 base_qual_type.getTypePtr()) 3197 return 0; 3198 } 3199 } 3200 } else if (field_qual_type->isVectorType() || 3201 field_qual_type->isExtVectorType()) { 3202 if (num_fields == 0) { 3203 base_qual_type = field_qual_type; 3204 base_bitwidth = field_bitwidth; 3205 } else { 3206 if (is_hfa) 3207 return 0; 3208 is_hva = true; 3209 if (base_bitwidth != field_bitwidth) 3210 return 0; 3211 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr()) 3212 return 0; 3213 } 3214 } else 3215 return 0; 3216 ++num_fields; 3217 } 3218 if (base_type_ptr) 3219 *base_type_ptr = CompilerType(getASTContext(), base_qual_type); 3220 return num_fields; 3221 } 3222 } 3223 } 3224 break; 3225 3226 case clang::Type::Typedef: 3227 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type) 3228 ->getDecl() 3229 ->getUnderlyingType() 3230 .getAsOpaquePtr(), 3231 base_type_ptr); 3232 3233 case clang::Type::Auto: 3234 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type) 3235 ->getDeducedType() 3236 .getAsOpaquePtr(), 3237 base_type_ptr); 3238 3239 case clang::Type::Elaborated: 3240 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type) 3241 ->getNamedType() 3242 .getAsOpaquePtr(), 3243 base_type_ptr); 3244 default: 3245 break; 3246 } 3247 return 0; 3248} 3249 3250size_t ClangASTContext::GetNumberOfFunctionArguments( 3251 lldb::opaque_compiler_type_t type) { 3252 if (type) { 3253 clang::QualType qual_type(GetCanonicalQualType(type)); 3254 const clang::FunctionProtoType *func = 3255 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); 3256 if (func) 3257 return func->getNumParams(); 3258 } 3259 return 0; 3260} 3261 3262CompilerType 3263ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, 3264 const size_t index) { 3265 if (type) { 3266 clang::QualType qual_type(GetQualType(type)); 3267 const clang::FunctionProtoType *func = 3268 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); 3269 if (func) { 3270 if (index < func->getNumParams()) 3271 return CompilerType(getASTContext(), func->getParamType(index)); 3272 } 3273 } 3274 return CompilerType(); 3275} 3276 3277bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) { 3278 if (type) { 3279 clang::QualType qual_type(GetCanonicalQualType(type)); 3280 3281 if (qual_type->isFunctionPointerType()) 3282 return true; 3283 3284 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3285 switch (type_class) { 3286 default: 3287 break; 3288 case clang::Type::Typedef: 3289 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type) 3290 ->getDecl() 3291 ->getUnderlyingType() 3292 .getAsOpaquePtr()); 3293 case clang::Type::Auto: 3294 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type) 3295 ->getDeducedType() 3296 .getAsOpaquePtr()); 3297 case clang::Type::Elaborated: 3298 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type) 3299 ->getNamedType() 3300 .getAsOpaquePtr()); 3301 case clang::Type::Paren: 3302 return IsFunctionPointerType( 3303 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); 3304 3305 case clang::Type::LValueReference: 3306 case clang::Type::RValueReference: { 3307 const clang::ReferenceType *reference_type = 3308 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); 3309 if (reference_type) 3310 return IsFunctionPointerType( 3311 reference_type->getPointeeType().getAsOpaquePtr()); 3312 } break; 3313 } 3314 } 3315 return false; 3316} 3317 3318bool ClangASTContext::IsBlockPointerType( 3319 lldb::opaque_compiler_type_t type, 3320 CompilerType *function_pointer_type_ptr) { 3321 if (type) { 3322 clang::QualType qual_type(GetCanonicalQualType(type)); 3323 3324 if (qual_type->isBlockPointerType()) { 3325 if (function_pointer_type_ptr) { 3326 const clang::BlockPointerType *block_pointer_type = 3327 qual_type->getAs<clang::BlockPointerType>(); 3328 QualType pointee_type = block_pointer_type->getPointeeType(); 3329 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type); 3330 *function_pointer_type_ptr = 3331 CompilerType(getASTContext(), function_pointer_type); 3332 } 3333 return true; 3334 } 3335 3336 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3337 switch (type_class) { 3338 default: 3339 break; 3340 case clang::Type::Typedef: 3341 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type) 3342 ->getDecl() 3343 ->getUnderlyingType() 3344 .getAsOpaquePtr(), 3345 function_pointer_type_ptr); 3346 case clang::Type::Auto: 3347 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type) 3348 ->getDeducedType() 3349 .getAsOpaquePtr(), 3350 function_pointer_type_ptr); 3351 case clang::Type::Elaborated: 3352 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type) 3353 ->getNamedType() 3354 .getAsOpaquePtr(), 3355 function_pointer_type_ptr); 3356 case clang::Type::Paren: 3357 return IsBlockPointerType( 3358 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), 3359 function_pointer_type_ptr); 3360 3361 case clang::Type::LValueReference: 3362 case clang::Type::RValueReference: { 3363 const clang::ReferenceType *reference_type = 3364 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); 3365 if (reference_type) 3366 return IsBlockPointerType( 3367 reference_type->getPointeeType().getAsOpaquePtr(), 3368 function_pointer_type_ptr); 3369 } break; 3370 } 3371 } 3372 return false; 3373} 3374 3375bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type, 3376 bool &is_signed) { 3377 if (!type) 3378 return false; 3379 3380 clang::QualType qual_type(GetCanonicalQualType(type)); 3381 const clang::BuiltinType *builtin_type = 3382 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()); 3383 3384 if (builtin_type) { 3385 if (builtin_type->isInteger()) { 3386 is_signed = builtin_type->isSignedInteger(); 3387 return true; 3388 } 3389 } 3390 3391 return false; 3392} 3393 3394bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type, 3395 bool &is_signed) { 3396 if (type) { 3397 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>( 3398 GetCanonicalQualType(type)->getCanonicalTypeInternal()); 3399 3400 if (enum_type) { 3401 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(), 3402 is_signed); 3403 return true; 3404 } 3405 } 3406 3407 return false; 3408} 3409 3410bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type, 3411 CompilerType *pointee_type) { 3412 if (type) { 3413 clang::QualType qual_type(GetCanonicalQualType(type)); 3414 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3415 switch (type_class) { 3416 case clang::Type::Builtin: 3417 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { 3418 default: 3419 break; 3420 case clang::BuiltinType::ObjCId: 3421 case clang::BuiltinType::ObjCClass: 3422 return true; 3423 } 3424 return false; 3425 case clang::Type::ObjCObjectPointer: 3426 if (pointee_type) 3427 pointee_type->SetCompilerType( 3428 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type) 3429 ->getPointeeType()); 3430 return true; 3431 case clang::Type::BlockPointer: 3432 if (pointee_type) 3433 pointee_type->SetCompilerType( 3434 getASTContext(), 3435 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType()); 3436 return true; 3437 case clang::Type::Pointer: 3438 if (pointee_type) 3439 pointee_type->SetCompilerType( 3440 getASTContext(), 3441 llvm::cast<clang::PointerType>(qual_type)->getPointeeType()); 3442 return true; 3443 case clang::Type::MemberPointer: 3444 if (pointee_type) 3445 pointee_type->SetCompilerType( 3446 getASTContext(), 3447 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType()); 3448 return true; 3449 case clang::Type::Typedef: 3450 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type) 3451 ->getDecl() 3452 ->getUnderlyingType() 3453 .getAsOpaquePtr(), 3454 pointee_type); 3455 case clang::Type::Auto: 3456 return IsPointerType(llvm::cast<clang::AutoType>(qual_type) 3457 ->getDeducedType() 3458 .getAsOpaquePtr(), 3459 pointee_type); 3460 case clang::Type::Elaborated: 3461 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type) 3462 ->getNamedType() 3463 .getAsOpaquePtr(), 3464 pointee_type); 3465 case clang::Type::Paren: 3466 return IsPointerType( 3467 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), 3468 pointee_type); 3469 default: 3470 break; 3471 } 3472 } 3473 if (pointee_type) 3474 pointee_type->Clear(); 3475 return false; 3476} 3477 3478bool ClangASTContext::IsPointerOrReferenceType( 3479 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) { 3480 if (type) { 3481 clang::QualType qual_type(GetCanonicalQualType(type)); 3482 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3483 switch (type_class) { 3484 case clang::Type::Builtin: 3485 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { 3486 default: 3487 break; 3488 case clang::BuiltinType::ObjCId: 3489 case clang::BuiltinType::ObjCClass: 3490 return true; 3491 } 3492 return false; 3493 case clang::Type::ObjCObjectPointer: 3494 if (pointee_type) 3495 pointee_type->SetCompilerType( 3496 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type) 3497 ->getPointeeType()); 3498 return true; 3499 case clang::Type::BlockPointer: 3500 if (pointee_type) 3501 pointee_type->SetCompilerType( 3502 getASTContext(), 3503 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType()); 3504 return true; 3505 case clang::Type::Pointer: 3506 if (pointee_type) 3507 pointee_type->SetCompilerType( 3508 getASTContext(), 3509 llvm::cast<clang::PointerType>(qual_type)->getPointeeType()); 3510 return true; 3511 case clang::Type::MemberPointer: 3512 if (pointee_type) 3513 pointee_type->SetCompilerType( 3514 getASTContext(), 3515 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType()); 3516 return true; 3517 case clang::Type::LValueReference: 3518 if (pointee_type) 3519 pointee_type->SetCompilerType( 3520 getASTContext(), 3521 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar()); 3522 return true; 3523 case clang::Type::RValueReference: 3524 if (pointee_type) 3525 pointee_type->SetCompilerType( 3526 getASTContext(), 3527 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar()); 3528 return true; 3529 case clang::Type::Typedef: 3530 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type) 3531 ->getDecl() 3532 ->getUnderlyingType() 3533 .getAsOpaquePtr(), 3534 pointee_type); 3535 case clang::Type::Auto: 3536 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type) 3537 ->getDeducedType() 3538 .getAsOpaquePtr(), 3539 pointee_type); 3540 case clang::Type::Elaborated: 3541 return IsPointerOrReferenceType( 3542 llvm::cast<clang::ElaboratedType>(qual_type) 3543 ->getNamedType() 3544 .getAsOpaquePtr(), 3545 pointee_type); 3546 case clang::Type::Paren: 3547 return IsPointerOrReferenceType( 3548 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), 3549 pointee_type); 3550 default: 3551 break; 3552 } 3553 } 3554 if (pointee_type) 3555 pointee_type->Clear(); 3556 return false; 3557} 3558 3559bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type, 3560 CompilerType *pointee_type, 3561 bool *is_rvalue) { 3562 if (type) { 3563 clang::QualType qual_type(GetCanonicalQualType(type)); 3564 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3565 3566 switch (type_class) { 3567 case clang::Type::LValueReference: 3568 if (pointee_type) 3569 pointee_type->SetCompilerType( 3570 getASTContext(), 3571 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar()); 3572 if (is_rvalue) 3573 *is_rvalue = false; 3574 return true; 3575 case clang::Type::RValueReference: 3576 if (pointee_type) 3577 pointee_type->SetCompilerType( 3578 getASTContext(), 3579 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar()); 3580 if (is_rvalue) 3581 *is_rvalue = true; 3582 return true; 3583 case clang::Type::Typedef: 3584 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type) 3585 ->getDecl() 3586 ->getUnderlyingType() 3587 .getAsOpaquePtr(), 3588 pointee_type, is_rvalue); 3589 case clang::Type::Auto: 3590 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type) 3591 ->getDeducedType() 3592 .getAsOpaquePtr(), 3593 pointee_type, is_rvalue); 3594 case clang::Type::Elaborated: 3595 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type) 3596 ->getNamedType() 3597 .getAsOpaquePtr(), 3598 pointee_type, is_rvalue); 3599 case clang::Type::Paren: 3600 return IsReferenceType( 3601 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), 3602 pointee_type, is_rvalue); 3603 3604 default: 3605 break; 3606 } 3607 } 3608 if (pointee_type) 3609 pointee_type->Clear(); 3610 return false; 3611} 3612 3613bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type, 3614 uint32_t &count, bool &is_complex) { 3615 if (type) { 3616 clang::QualType qual_type(GetCanonicalQualType(type)); 3617 3618 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>( 3619 qual_type->getCanonicalTypeInternal())) { 3620 clang::BuiltinType::Kind kind = BT->getKind(); 3621 if (kind >= clang::BuiltinType::Float && 3622 kind <= clang::BuiltinType::LongDouble) { 3623 count = 1; 3624 is_complex = false; 3625 return true; 3626 } 3627 } else if (const clang::ComplexType *CT = 3628 llvm::dyn_cast<clang::ComplexType>( 3629 qual_type->getCanonicalTypeInternal())) { 3630 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, 3631 is_complex)) { 3632 count = 2; 3633 is_complex = true; 3634 return true; 3635 } 3636 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>( 3637 qual_type->getCanonicalTypeInternal())) { 3638 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, 3639 is_complex)) { 3640 count = VT->getNumElements(); 3641 is_complex = false; 3642 return true; 3643 } 3644 } 3645 } 3646 count = 0; 3647 is_complex = false; 3648 return false; 3649} 3650 3651bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) { 3652 if (!type) 3653 return false; 3654 3655 clang::QualType qual_type(GetQualType(type)); 3656 const clang::TagType *tag_type = 3657 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()); 3658 if (tag_type) { 3659 clang::TagDecl *tag_decl = tag_type->getDecl(); 3660 if (tag_decl) 3661 return tag_decl->isCompleteDefinition(); 3662 return false; 3663 } else { 3664 const clang::ObjCObjectType *objc_class_type = 3665 llvm::dyn_cast<clang::ObjCObjectType>(qual_type); 3666 if (objc_class_type) { 3667 clang::ObjCInterfaceDecl *class_interface_decl = 3668 objc_class_type->getInterface(); 3669 if (class_interface_decl) 3670 return class_interface_decl->getDefinition() != nullptr; 3671 return false; 3672 } 3673 } 3674 return true; 3675} 3676 3677bool ClangASTContext::IsObjCClassType(const CompilerType &type) { 3678 if (type) { 3679 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); 3680 3681 const clang::ObjCObjectPointerType *obj_pointer_type = 3682 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type); 3683 3684 if (obj_pointer_type) 3685 return obj_pointer_type->isObjCClassType(); 3686 } 3687 return false; 3688} 3689 3690bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) { 3691 if (ClangUtil::IsClangType(type)) 3692 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType(); 3693 return false; 3694} 3695 3696bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) { 3697 if (!type) 3698 return false; 3699 clang::QualType qual_type(GetCanonicalQualType(type)); 3700 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3701 return (type_class == clang::Type::Record); 3702} 3703 3704bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) { 3705 if (!type) 3706 return false; 3707 clang::QualType qual_type(GetCanonicalQualType(type)); 3708 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3709 return (type_class == clang::Type::Enum); 3710} 3711 3712bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) { 3713 if (type) { 3714 clang::QualType qual_type(GetCanonicalQualType(type)); 3715 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3716 switch (type_class) { 3717 case clang::Type::Record: 3718 if (GetCompleteType(type)) { 3719 const clang::RecordType *record_type = 3720 llvm::cast<clang::RecordType>(qual_type.getTypePtr()); 3721 const clang::RecordDecl *record_decl = record_type->getDecl(); 3722 if (record_decl) { 3723 const clang::CXXRecordDecl *cxx_record_decl = 3724 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); 3725 if (cxx_record_decl) 3726 return cxx_record_decl->isPolymorphic(); 3727 } 3728 } 3729 break; 3730 3731 default: 3732 break; 3733 } 3734 } 3735 return false; 3736} 3737 3738bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type, 3739 CompilerType *dynamic_pointee_type, 3740 bool check_cplusplus, 3741 bool check_objc) { 3742 clang::QualType pointee_qual_type; 3743 if (type) { 3744 clang::QualType qual_type(GetCanonicalQualType(type)); 3745 bool success = false; 3746 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3747 switch (type_class) { 3748 case clang::Type::Builtin: 3749 if (check_objc && 3750 llvm::cast<clang::BuiltinType>(qual_type)->getKind() == 3751 clang::BuiltinType::ObjCId) { 3752 if (dynamic_pointee_type) 3753 dynamic_pointee_type->SetCompilerType(this, type); 3754 return true; 3755 } 3756 break; 3757 3758 case clang::Type::ObjCObjectPointer: 3759 if (check_objc) { 3760 if (auto objc_pointee_type = 3761 qual_type->getPointeeType().getTypePtrOrNull()) { 3762 if (auto objc_object_type = 3763 llvm::dyn_cast_or_null<clang::ObjCObjectType>( 3764 objc_pointee_type)) { 3765 if (objc_object_type->isObjCClass()) 3766 return false; 3767 } 3768 } 3769 if (dynamic_pointee_type) 3770 dynamic_pointee_type->SetCompilerType( 3771 getASTContext(), 3772 llvm::cast<clang::ObjCObjectPointerType>(qual_type) 3773 ->getPointeeType()); 3774 return true; 3775 } 3776 break; 3777 3778 case clang::Type::Pointer: 3779 pointee_qual_type = 3780 llvm::cast<clang::PointerType>(qual_type)->getPointeeType(); 3781 success = true; 3782 break; 3783 3784 case clang::Type::LValueReference: 3785 case clang::Type::RValueReference: 3786 pointee_qual_type = 3787 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType(); 3788 success = true; 3789 break; 3790 3791 case clang::Type::Typedef: 3792 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type) 3793 ->getDecl() 3794 ->getUnderlyingType() 3795 .getAsOpaquePtr(), 3796 dynamic_pointee_type, check_cplusplus, 3797 check_objc); 3798 3799 case clang::Type::Auto: 3800 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type) 3801 ->getDeducedType() 3802 .getAsOpaquePtr(), 3803 dynamic_pointee_type, check_cplusplus, 3804 check_objc); 3805 3806 case clang::Type::Elaborated: 3807 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type) 3808 ->getNamedType() 3809 .getAsOpaquePtr(), 3810 dynamic_pointee_type, check_cplusplus, 3811 check_objc); 3812 3813 case clang::Type::Paren: 3814 return IsPossibleDynamicType( 3815 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), 3816 dynamic_pointee_type, check_cplusplus, check_objc); 3817 default: 3818 break; 3819 } 3820 3821 if (success) { 3822 // Check to make sure what we are pointing too is a possible dynamic C++ 3823 // type We currently accept any "void *" (in case we have a class that 3824 // has been watered down to an opaque pointer) and virtual C++ classes. 3825 const clang::Type::TypeClass pointee_type_class = 3826 pointee_qual_type.getCanonicalType()->getTypeClass(); 3827 switch (pointee_type_class) { 3828 case clang::Type::Builtin: 3829 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) { 3830 case clang::BuiltinType::UnknownAny: 3831 case clang::BuiltinType::Void: 3832 if (dynamic_pointee_type) 3833 dynamic_pointee_type->SetCompilerType(getASTContext(), 3834 pointee_qual_type); 3835 return true; 3836 default: 3837 break; 3838 } 3839 break; 3840 3841 case clang::Type::Record: 3842 if (check_cplusplus) { 3843 clang::CXXRecordDecl *cxx_record_decl = 3844 pointee_qual_type->getAsCXXRecordDecl(); 3845 if (cxx_record_decl) { 3846 bool is_complete = cxx_record_decl->isCompleteDefinition(); 3847 3848 if (is_complete) 3849 success = cxx_record_decl->isDynamicClass(); 3850 else { 3851 ClangASTMetadata *metadata = ClangASTContext::GetMetadata( 3852 getASTContext(), cxx_record_decl); 3853 if (metadata) 3854 success = metadata->GetIsDynamicCXXType(); 3855 else { 3856 is_complete = CompilerType(getASTContext(), pointee_qual_type) 3857 .GetCompleteType(); 3858 if (is_complete) 3859 success = cxx_record_decl->isDynamicClass(); 3860 else 3861 success = false; 3862 } 3863 } 3864 3865 if (success) { 3866 if (dynamic_pointee_type) 3867 dynamic_pointee_type->SetCompilerType(getASTContext(), 3868 pointee_qual_type); 3869 return true; 3870 } 3871 } 3872 } 3873 break; 3874 3875 case clang::Type::ObjCObject: 3876 case clang::Type::ObjCInterface: 3877 if (check_objc) { 3878 if (dynamic_pointee_type) 3879 dynamic_pointee_type->SetCompilerType(getASTContext(), 3880 pointee_qual_type); 3881 return true; 3882 } 3883 break; 3884 3885 default: 3886 break; 3887 } 3888 } 3889 } 3890 if (dynamic_pointee_type) 3891 dynamic_pointee_type->Clear(); 3892 return false; 3893} 3894 3895bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) { 3896 if (!type) 3897 return false; 3898 3899 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0; 3900} 3901 3902bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) { 3903 if (!type) 3904 return false; 3905 return GetQualType(type)->getTypeClass() == clang::Type::Typedef; 3906} 3907 3908bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) { 3909 if (!type) 3910 return false; 3911 return GetCanonicalQualType(type)->isVoidType(); 3912} 3913 3914bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) { 3915 return ClangASTContextSupportsLanguage(language); 3916} 3917 3918bool ClangASTContext::GetCXXClassName(const CompilerType &type, 3919 std::string &class_name) { 3920 if (type) { 3921 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); 3922 if (!qual_type.isNull()) { 3923 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 3924 if (cxx_record_decl) { 3925 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart()); 3926 return true; 3927 } 3928 } 3929 } 3930 class_name.clear(); 3931 return false; 3932} 3933 3934bool ClangASTContext::IsCXXClassType(const CompilerType &type) { 3935 if (!type) 3936 return false; 3937 3938 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); 3939 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr; 3940} 3941 3942bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) { 3943 if (!type) 3944 return false; 3945 clang::QualType qual_type(GetCanonicalQualType(type)); 3946 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type); 3947 if (tag_type) 3948 return tag_type->isBeingDefined(); 3949 return false; 3950} 3951 3952bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type, 3953 CompilerType *class_type_ptr) { 3954 if (!type) 3955 return false; 3956 3957 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); 3958 3959 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) { 3960 if (class_type_ptr) { 3961 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) { 3962 const clang::ObjCObjectPointerType *obj_pointer_type = 3963 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type); 3964 if (obj_pointer_type == nullptr) 3965 class_type_ptr->Clear(); 3966 else 3967 class_type_ptr->SetCompilerType( 3968 type.GetTypeSystem(), 3969 clang::QualType(obj_pointer_type->getInterfaceType(), 0) 3970 .getAsOpaquePtr()); 3971 } 3972 } 3973 return true; 3974 } 3975 if (class_type_ptr) 3976 class_type_ptr->Clear(); 3977 return false; 3978} 3979 3980bool ClangASTContext::GetObjCClassName(const CompilerType &type, 3981 std::string &class_name) { 3982 if (!type) 3983 return false; 3984 3985 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); 3986 3987 const clang::ObjCObjectType *object_type = 3988 llvm::dyn_cast<clang::ObjCObjectType>(qual_type); 3989 if (object_type) { 3990 const clang::ObjCInterfaceDecl *interface = object_type->getInterface(); 3991 if (interface) { 3992 class_name = interface->getNameAsString(); 3993 return true; 3994 } 3995 } 3996 return false; 3997} 3998 3999//---------------------------------------------------------------------- 4000// Type Completion 4001//---------------------------------------------------------------------- 4002 4003bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) { 4004 if (!type) 4005 return false; 4006 const bool allow_completion = true; 4007 return GetCompleteQualType(getASTContext(), GetQualType(type), 4008 allow_completion); 4009} 4010 4011ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) { 4012 std::string type_name; 4013 if (type) { 4014 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy()); 4015 clang::QualType qual_type(GetQualType(type)); 4016 printing_policy.SuppressTagKeyword = true; 4017 const clang::TypedefType *typedef_type = 4018 qual_type->getAs<clang::TypedefType>(); 4019 if (typedef_type) { 4020 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl(); 4021 type_name = typedef_decl->getQualifiedNameAsString(); 4022 } else { 4023 type_name = qual_type.getAsString(printing_policy); 4024 } 4025 } 4026 return ConstString(type_name); 4027} 4028 4029uint32_t 4030ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type, 4031 CompilerType *pointee_or_element_clang_type) { 4032 if (!type) 4033 return 0; 4034 4035 if (pointee_or_element_clang_type) 4036 pointee_or_element_clang_type->Clear(); 4037 4038 clang::QualType qual_type(GetQualType(type)); 4039 4040 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4041 switch (type_class) { 4042 case clang::Type::Attributed: 4043 return GetTypeInfo( 4044 qual_type->getAs<clang::AttributedType>() 4045 ->getModifiedType().getAsOpaquePtr(), 4046 pointee_or_element_clang_type); 4047 case clang::Type::Builtin: { 4048 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>( 4049 qual_type->getCanonicalTypeInternal()); 4050 4051 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue; 4052 switch (builtin_type->getKind()) { 4053 case clang::BuiltinType::ObjCId: 4054 case clang::BuiltinType::ObjCClass: 4055 if (pointee_or_element_clang_type) 4056 pointee_or_element_clang_type->SetCompilerType( 4057 getASTContext(), getASTContext()->ObjCBuiltinClassTy); 4058 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC; 4059 break; 4060 4061 case clang::BuiltinType::ObjCSel: 4062 if (pointee_or_element_clang_type) 4063 pointee_or_element_clang_type->SetCompilerType(getASTContext(), 4064 getASTContext()->CharTy); 4065 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC; 4066 break; 4067 4068 case clang::BuiltinType::Bool: 4069 case clang::BuiltinType::Char_U: 4070 case clang::BuiltinType::UChar: 4071 case clang::BuiltinType::WChar_U: 4072 case clang::BuiltinType::Char16: 4073 case clang::BuiltinType::Char32: 4074 case clang::BuiltinType::UShort: 4075 case clang::BuiltinType::UInt: 4076 case clang::BuiltinType::ULong: 4077 case clang::BuiltinType::ULongLong: 4078 case clang::BuiltinType::UInt128: 4079 case clang::BuiltinType::Char_S: 4080 case clang::BuiltinType::SChar: 4081 case clang::BuiltinType::WChar_S: 4082 case clang::BuiltinType::Short: 4083 case clang::BuiltinType::Int: 4084 case clang::BuiltinType::Long: 4085 case clang::BuiltinType::LongLong: 4086 case clang::BuiltinType::Int128: 4087 case clang::BuiltinType::Float: 4088 case clang::BuiltinType::Double: 4089 case clang::BuiltinType::LongDouble: 4090 builtin_type_flags |= eTypeIsScalar; 4091 if (builtin_type->isInteger()) { 4092 builtin_type_flags |= eTypeIsInteger; 4093 if (builtin_type->isSignedInteger()) 4094 builtin_type_flags |= eTypeIsSigned; 4095 } else if (builtin_type->isFloatingPoint()) 4096 builtin_type_flags |= eTypeIsFloat; 4097 break; 4098 default: 4099 break; 4100 } 4101 return builtin_type_flags; 4102 } 4103 4104 case clang::Type::BlockPointer: 4105 if (pointee_or_element_clang_type) 4106 pointee_or_element_clang_type->SetCompilerType( 4107 getASTContext(), qual_type->getPointeeType()); 4108 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock; 4109 4110 case clang::Type::Complex: { 4111 uint32_t complex_type_flags = 4112 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex; 4113 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>( 4114 qual_type->getCanonicalTypeInternal()); 4115 if (complex_type) { 4116 clang::QualType complex_element_type(complex_type->getElementType()); 4117 if (complex_element_type->isIntegerType()) 4118 complex_type_flags |= eTypeIsFloat; 4119 else if (complex_element_type->isFloatingType()) 4120 complex_type_flags |= eTypeIsInteger; 4121 } 4122 return complex_type_flags; 4123 } break; 4124 4125 case clang::Type::ConstantArray: 4126 case clang::Type::DependentSizedArray: 4127 case clang::Type::IncompleteArray: 4128 case clang::Type::VariableArray: 4129 if (pointee_or_element_clang_type) 4130 pointee_or_element_clang_type->SetCompilerType( 4131 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr()) 4132 ->getElementType()); 4133 return eTypeHasChildren | eTypeIsArray; 4134 4135 case clang::Type::DependentName: 4136 return 0; 4137 case clang::Type::DependentSizedExtVector: 4138 return eTypeHasChildren | eTypeIsVector; 4139 case clang::Type::DependentTemplateSpecialization: 4140 return eTypeIsTemplate; 4141 case clang::Type::Decltype: 4142 return CompilerType( 4143 getASTContext(), 4144 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()) 4145 .GetTypeInfo(pointee_or_element_clang_type); 4146 4147 case clang::Type::Enum: 4148 if (pointee_or_element_clang_type) 4149 pointee_or_element_clang_type->SetCompilerType( 4150 getASTContext(), 4151 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType()); 4152 return eTypeIsEnumeration | eTypeHasValue; 4153 4154 case clang::Type::Auto: 4155 return CompilerType( 4156 getASTContext(), 4157 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) 4158 .GetTypeInfo(pointee_or_element_clang_type); 4159 case clang::Type::Elaborated: 4160 return CompilerType( 4161 getASTContext(), 4162 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) 4163 .GetTypeInfo(pointee_or_element_clang_type); 4164 case clang::Type::Paren: 4165 return CompilerType(getASTContext(), 4166 llvm::cast<clang::ParenType>(qual_type)->desugar()) 4167 .GetTypeInfo(pointee_or_element_clang_type); 4168 4169 case clang::Type::FunctionProto: 4170 return eTypeIsFuncPrototype | eTypeHasValue; 4171 case clang::Type::FunctionNoProto: 4172 return eTypeIsFuncPrototype | eTypeHasValue; 4173 case clang::Type::InjectedClassName: 4174 return 0; 4175 4176 case clang::Type::LValueReference: 4177 case clang::Type::RValueReference: 4178 if (pointee_or_element_clang_type) 4179 pointee_or_element_clang_type->SetCompilerType( 4180 getASTContext(), 4181 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()) 4182 ->getPointeeType()); 4183 return eTypeHasChildren | eTypeIsReference | eTypeHasValue; 4184 4185 case clang::Type::MemberPointer: 4186 return eTypeIsPointer | eTypeIsMember | eTypeHasValue; 4187 4188 case clang::Type::ObjCObjectPointer: 4189 if (pointee_or_element_clang_type) 4190 pointee_or_element_clang_type->SetCompilerType( 4191 getASTContext(), qual_type->getPointeeType()); 4192 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | 4193 eTypeHasValue; 4194 4195 case clang::Type::ObjCObject: 4196 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; 4197 case clang::Type::ObjCInterface: 4198 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; 4199 4200 case clang::Type::Pointer: 4201 if (pointee_or_element_clang_type) 4202 pointee_or_element_clang_type->SetCompilerType( 4203 getASTContext(), qual_type->getPointeeType()); 4204 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue; 4205 4206 case clang::Type::Record: 4207 if (qual_type->getAsCXXRecordDecl()) 4208 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus; 4209 else 4210 return eTypeHasChildren | eTypeIsStructUnion; 4211 break; 4212 case clang::Type::SubstTemplateTypeParm: 4213 return eTypeIsTemplate; 4214 case clang::Type::TemplateTypeParm: 4215 return eTypeIsTemplate; 4216 case clang::Type::TemplateSpecialization: 4217 return eTypeIsTemplate; 4218 4219 case clang::Type::Typedef: 4220 return eTypeIsTypedef | 4221 CompilerType(getASTContext(), 4222 llvm::cast<clang::TypedefType>(qual_type) 4223 ->getDecl() 4224 ->getUnderlyingType()) 4225 .GetTypeInfo(pointee_or_element_clang_type); 4226 case clang::Type::TypeOfExpr: 4227 return CompilerType(getASTContext(), 4228 llvm::cast<clang::TypeOfExprType>(qual_type) 4229 ->getUnderlyingExpr() 4230 ->getType()) 4231 .GetTypeInfo(pointee_or_element_clang_type); 4232 case clang::Type::TypeOf: 4233 return CompilerType( 4234 getASTContext(), 4235 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) 4236 .GetTypeInfo(pointee_or_element_clang_type); 4237 case clang::Type::UnresolvedUsing: 4238 return 0; 4239 4240 case clang::Type::ExtVector: 4241 case clang::Type::Vector: { 4242 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector; 4243 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>( 4244 qual_type->getCanonicalTypeInternal()); 4245 if (vector_type) { 4246 if (vector_type->isIntegerType()) 4247 vector_type_flags |= eTypeIsFloat; 4248 else if (vector_type->isFloatingType()) 4249 vector_type_flags |= eTypeIsInteger; 4250 } 4251 return vector_type_flags; 4252 } 4253 default: 4254 return 0; 4255 } 4256 return 0; 4257} 4258 4259lldb::LanguageType 4260ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) { 4261 if (!type) 4262 return lldb::eLanguageTypeC; 4263 4264 // If the type is a reference, then resolve it to what it refers to first: 4265 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType()); 4266 if (qual_type->isAnyPointerType()) { 4267 if (qual_type->isObjCObjectPointerType()) 4268 return lldb::eLanguageTypeObjC; 4269 4270 clang::QualType pointee_type(qual_type->getPointeeType()); 4271 if (pointee_type->getPointeeCXXRecordDecl() != nullptr) 4272 return lldb::eLanguageTypeC_plus_plus; 4273 if (pointee_type->isObjCObjectOrInterfaceType()) 4274 return lldb::eLanguageTypeObjC; 4275 if (pointee_type->isObjCClassType()) 4276 return lldb::eLanguageTypeObjC; 4277 if (pointee_type.getTypePtr() == 4278 getASTContext()->ObjCBuiltinIdTy.getTypePtr()) 4279 return lldb::eLanguageTypeObjC; 4280 } else { 4281 if (qual_type->isObjCObjectOrInterfaceType()) 4282 return lldb::eLanguageTypeObjC; 4283 if (qual_type->getAsCXXRecordDecl()) 4284 return lldb::eLanguageTypeC_plus_plus; 4285 switch (qual_type->getTypeClass()) { 4286 default: 4287 break; 4288 case clang::Type::Builtin: 4289 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { 4290 default: 4291 case clang::BuiltinType::Void: 4292 case clang::BuiltinType::Bool: 4293 case clang::BuiltinType::Char_U: 4294 case clang::BuiltinType::UChar: 4295 case clang::BuiltinType::WChar_U: 4296 case clang::BuiltinType::Char16: 4297 case clang::BuiltinType::Char32: 4298 case clang::BuiltinType::UShort: 4299 case clang::BuiltinType::UInt: 4300 case clang::BuiltinType::ULong: 4301 case clang::BuiltinType::ULongLong: 4302 case clang::BuiltinType::UInt128: 4303 case clang::BuiltinType::Char_S: 4304 case clang::BuiltinType::SChar: 4305 case clang::BuiltinType::WChar_S: 4306 case clang::BuiltinType::Short: 4307 case clang::BuiltinType::Int: 4308 case clang::BuiltinType::Long: 4309 case clang::BuiltinType::LongLong: 4310 case clang::BuiltinType::Int128: 4311 case clang::BuiltinType::Float: 4312 case clang::BuiltinType::Double: 4313 case clang::BuiltinType::LongDouble: 4314 break; 4315 4316 case clang::BuiltinType::NullPtr: 4317 return eLanguageTypeC_plus_plus; 4318 4319 case clang::BuiltinType::ObjCId: 4320 case clang::BuiltinType::ObjCClass: 4321 case clang::BuiltinType::ObjCSel: 4322 return eLanguageTypeObjC; 4323 4324 case clang::BuiltinType::Dependent: 4325 case clang::BuiltinType::Overload: 4326 case clang::BuiltinType::BoundMember: 4327 case clang::BuiltinType::UnknownAny: 4328 break; 4329 } 4330 break; 4331 case clang::Type::Typedef: 4332 return CompilerType(getASTContext(), 4333 llvm::cast<clang::TypedefType>(qual_type) 4334 ->getDecl() 4335 ->getUnderlyingType()) 4336 .GetMinimumLanguage(); 4337 } 4338 } 4339 return lldb::eLanguageTypeC; 4340} 4341 4342lldb::TypeClass 4343ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) { 4344 if (!type) 4345 return lldb::eTypeClassInvalid; 4346 4347 clang::QualType qual_type(GetQualType(type)); 4348 4349 switch (qual_type->getTypeClass()) { 4350 case clang::Type::UnaryTransform: 4351 break; 4352 case clang::Type::FunctionNoProto: 4353 return lldb::eTypeClassFunction; 4354 case clang::Type::FunctionProto: 4355 return lldb::eTypeClassFunction; 4356 case clang::Type::IncompleteArray: 4357 return lldb::eTypeClassArray; 4358 case clang::Type::VariableArray: 4359 return lldb::eTypeClassArray; 4360 case clang::Type::ConstantArray: 4361 return lldb::eTypeClassArray; 4362 case clang::Type::DependentSizedArray: 4363 return lldb::eTypeClassArray; 4364 case clang::Type::DependentSizedExtVector: 4365 return lldb::eTypeClassVector; 4366 case clang::Type::DependentVector: 4367 return lldb::eTypeClassVector; 4368 case clang::Type::ExtVector: 4369 return lldb::eTypeClassVector; 4370 case clang::Type::Vector: 4371 return lldb::eTypeClassVector; 4372 case clang::Type::Builtin: 4373 return lldb::eTypeClassBuiltin; 4374 case clang::Type::ObjCObjectPointer: 4375 return lldb::eTypeClassObjCObjectPointer; 4376 case clang::Type::BlockPointer: 4377 return lldb::eTypeClassBlockPointer; 4378 case clang::Type::Pointer: 4379 return lldb::eTypeClassPointer; 4380 case clang::Type::LValueReference: 4381 return lldb::eTypeClassReference; 4382 case clang::Type::RValueReference: 4383 return lldb::eTypeClassReference; 4384 case clang::Type::MemberPointer: 4385 return lldb::eTypeClassMemberPointer; 4386 case clang::Type::Complex: 4387 if (qual_type->isComplexType()) 4388 return lldb::eTypeClassComplexFloat; 4389 else 4390 return lldb::eTypeClassComplexInteger; 4391 case clang::Type::ObjCObject: 4392 return lldb::eTypeClassObjCObject; 4393 case clang::Type::ObjCInterface: 4394 return lldb::eTypeClassObjCInterface; 4395 case clang::Type::Record: { 4396 const clang::RecordType *record_type = 4397 llvm::cast<clang::RecordType>(qual_type.getTypePtr()); 4398 const clang::RecordDecl *record_decl = record_type->getDecl(); 4399 if (record_decl->isUnion()) 4400 return lldb::eTypeClassUnion; 4401 else if (record_decl->isStruct()) 4402 return lldb::eTypeClassStruct; 4403 else 4404 return lldb::eTypeClassClass; 4405 } break; 4406 case clang::Type::Enum: 4407 return lldb::eTypeClassEnumeration; 4408 case clang::Type::Typedef: 4409 return lldb::eTypeClassTypedef; 4410 case clang::Type::UnresolvedUsing: 4411 break; 4412 case clang::Type::Paren: 4413 return CompilerType(getASTContext(), 4414 llvm::cast<clang::ParenType>(qual_type)->desugar()) 4415 .GetTypeClass(); 4416 case clang::Type::Auto: 4417 return CompilerType( 4418 getASTContext(), 4419 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) 4420 .GetTypeClass(); 4421 case clang::Type::Elaborated: 4422 return CompilerType( 4423 getASTContext(), 4424 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) 4425 .GetTypeClass(); 4426 4427 case clang::Type::Attributed: 4428 break; 4429 case clang::Type::TemplateTypeParm: 4430 break; 4431 case clang::Type::SubstTemplateTypeParm: 4432 break; 4433 case clang::Type::SubstTemplateTypeParmPack: 4434 break; 4435 case clang::Type::InjectedClassName: 4436 break; 4437 case clang::Type::DependentName: 4438 break; 4439 case clang::Type::DependentTemplateSpecialization: 4440 break; 4441 case clang::Type::PackExpansion: 4442 break; 4443 4444 case clang::Type::TypeOfExpr: 4445 return CompilerType(getASTContext(), 4446 llvm::cast<clang::TypeOfExprType>(qual_type) 4447 ->getUnderlyingExpr() 4448 ->getType()) 4449 .GetTypeClass(); 4450 case clang::Type::TypeOf: 4451 return CompilerType( 4452 getASTContext(), 4453 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) 4454 .GetTypeClass(); 4455 case clang::Type::Decltype: 4456 return CompilerType( 4457 getASTContext(), 4458 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) 4459 .GetTypeClass(); 4460 case clang::Type::TemplateSpecialization: 4461 break; 4462 case clang::Type::DeducedTemplateSpecialization: 4463 break; 4464 case clang::Type::Atomic: 4465 break; 4466 case clang::Type::Pipe: 4467 break; 4468 4469 // pointer type decayed from an array or function type. 4470 case clang::Type::Decayed: 4471 break; 4472 case clang::Type::Adjusted: 4473 break; 4474 case clang::Type::ObjCTypeParam: 4475 break; 4476 4477 case clang::Type::DependentAddressSpace: 4478 break; 4479 } 4480 // We don't know hot to display this type... 4481 return lldb::eTypeClassOther; 4482} 4483 4484unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) { 4485 if (type) 4486 return GetQualType(type).getQualifiers().getCVRQualifiers(); 4487 return 0; 4488} 4489 4490//---------------------------------------------------------------------- 4491// Creating related types 4492//---------------------------------------------------------------------- 4493 4494CompilerType 4495ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type, 4496 uint64_t *stride) { 4497 if (type) { 4498 clang::QualType qual_type(GetCanonicalQualType(type)); 4499 4500 const clang::Type *array_eletype = 4501 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual(); 4502 4503 if (!array_eletype) 4504 return CompilerType(); 4505 4506 CompilerType element_type(getASTContext(), 4507 array_eletype->getCanonicalTypeUnqualified()); 4508 4509 // TODO: the real stride will be >= this value.. find the real one! 4510 if (stride) 4511 if (Optional<uint64_t> size = element_type.GetByteSize(nullptr)) 4512 *stride = *size; 4513 4514 return element_type; 4515 } 4516 return CompilerType(); 4517} 4518 4519CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type, 4520 uint64_t size) { 4521 if (type) { 4522 clang::QualType qual_type(GetCanonicalQualType(type)); 4523 if (clang::ASTContext *ast_ctx = getASTContext()) { 4524 if (size != 0) 4525 return CompilerType( 4526 ast_ctx, ast_ctx->getConstantArrayType( 4527 qual_type, llvm::APInt(64, size), 4528 clang::ArrayType::ArraySizeModifier::Normal, 0)); 4529 else 4530 return CompilerType( 4531 ast_ctx, 4532 ast_ctx->getIncompleteArrayType( 4533 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0)); 4534 } 4535 } 4536 4537 return CompilerType(); 4538} 4539 4540CompilerType 4541ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) { 4542 if (type) 4543 return CompilerType(getASTContext(), GetCanonicalQualType(type)); 4544 return CompilerType(); 4545} 4546 4547static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast, 4548 clang::QualType qual_type) { 4549 if (qual_type->isPointerType()) 4550 qual_type = ast->getPointerType( 4551 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType())); 4552 else 4553 qual_type = qual_type.getUnqualifiedType(); 4554 qual_type.removeLocalConst(); 4555 qual_type.removeLocalRestrict(); 4556 qual_type.removeLocalVolatile(); 4557 return qual_type; 4558} 4559 4560CompilerType 4561ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) { 4562 if (type) 4563 return CompilerType( 4564 getASTContext(), 4565 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type))); 4566 return CompilerType(); 4567} 4568 4569int ClangASTContext::GetFunctionArgumentCount( 4570 lldb::opaque_compiler_type_t type) { 4571 if (type) { 4572 const clang::FunctionProtoType *func = 4573 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type)); 4574 if (func) 4575 return func->getNumParams(); 4576 } 4577 return -1; 4578} 4579 4580CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex( 4581 lldb::opaque_compiler_type_t type, size_t idx) { 4582 if (type) { 4583 const clang::FunctionProtoType *func = 4584 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type)); 4585 if (func) { 4586 const uint32_t num_args = func->getNumParams(); 4587 if (idx < num_args) 4588 return CompilerType(getASTContext(), func->getParamType(idx)); 4589 } 4590 } 4591 return CompilerType(); 4592} 4593 4594CompilerType 4595ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) { 4596 if (type) { 4597 clang::QualType qual_type(GetQualType(type)); 4598 const clang::FunctionProtoType *func = 4599 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); 4600 if (func) 4601 return CompilerType(getASTContext(), func->getReturnType()); 4602 } 4603 return CompilerType(); 4604} 4605 4606size_t 4607ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) { 4608 size_t num_functions = 0; 4609 if (type) { 4610 clang::QualType qual_type(GetCanonicalQualType(type)); 4611 switch (qual_type->getTypeClass()) { 4612 case clang::Type::Record: 4613 if (GetCompleteQualType(getASTContext(), qual_type)) { 4614 const clang::RecordType *record_type = 4615 llvm::cast<clang::RecordType>(qual_type.getTypePtr()); 4616 const clang::RecordDecl *record_decl = record_type->getDecl(); 4617 assert(record_decl); 4618 const clang::CXXRecordDecl *cxx_record_decl = 4619 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); 4620 if (cxx_record_decl) 4621 num_functions = std::distance(cxx_record_decl->method_begin(), 4622 cxx_record_decl->method_end()); 4623 } 4624 break; 4625 4626 case clang::Type::ObjCObjectPointer: { 4627 const clang::ObjCObjectPointerType *objc_class_type = 4628 qual_type->getAs<clang::ObjCObjectPointerType>(); 4629 const clang::ObjCInterfaceType *objc_interface_type = 4630 objc_class_type->getInterfaceType(); 4631 if (objc_interface_type && 4632 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( 4633 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { 4634 clang::ObjCInterfaceDecl *class_interface_decl = 4635 objc_interface_type->getDecl(); 4636 if (class_interface_decl) { 4637 num_functions = std::distance(class_interface_decl->meth_begin(), 4638 class_interface_decl->meth_end()); 4639 } 4640 } 4641 break; 4642 } 4643 4644 case clang::Type::ObjCObject: 4645 case clang::Type::ObjCInterface: 4646 if (GetCompleteType(type)) { 4647 const clang::ObjCObjectType *objc_class_type = 4648 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); 4649 if (objc_class_type) { 4650 clang::ObjCInterfaceDecl *class_interface_decl = 4651 objc_class_type->getInterface(); 4652 if (class_interface_decl) 4653 num_functions = std::distance(class_interface_decl->meth_begin(), 4654 class_interface_decl->meth_end()); 4655 } 4656 } 4657 break; 4658 4659 case clang::Type::Typedef: 4660 return CompilerType(getASTContext(), 4661 llvm::cast<clang::TypedefType>(qual_type) 4662 ->getDecl() 4663 ->getUnderlyingType()) 4664 .GetNumMemberFunctions(); 4665 4666 case clang::Type::Auto: 4667 return CompilerType( 4668 getASTContext(), 4669 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) 4670 .GetNumMemberFunctions(); 4671 4672 case clang::Type::Elaborated: 4673 return CompilerType( 4674 getASTContext(), 4675 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) 4676 .GetNumMemberFunctions(); 4677 4678 case clang::Type::Paren: 4679 return CompilerType(getASTContext(), 4680 llvm::cast<clang::ParenType>(qual_type)->desugar()) 4681 .GetNumMemberFunctions(); 4682 4683 default: 4684 break; 4685 } 4686 } 4687 return num_functions; 4688} 4689 4690TypeMemberFunctionImpl 4691ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, 4692 size_t idx) { 4693 std::string name; 4694 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown); 4695 CompilerType clang_type; 4696 CompilerDecl clang_decl; 4697 if (type) { 4698 clang::QualType qual_type(GetCanonicalQualType(type)); 4699 switch (qual_type->getTypeClass()) { 4700 case clang::Type::Record: 4701 if (GetCompleteQualType(getASTContext(), qual_type)) { 4702 const clang::RecordType *record_type = 4703 llvm::cast<clang::RecordType>(qual_type.getTypePtr()); 4704 const clang::RecordDecl *record_decl = record_type->getDecl(); 4705 assert(record_decl); 4706 const clang::CXXRecordDecl *cxx_record_decl = 4707 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); 4708 if (cxx_record_decl) { 4709 auto method_iter = cxx_record_decl->method_begin(); 4710 auto method_end = cxx_record_decl->method_end(); 4711 if (idx < 4712 static_cast<size_t>(std::distance(method_iter, method_end))) { 4713 std::advance(method_iter, idx); 4714 clang::CXXMethodDecl *cxx_method_decl = 4715 method_iter->getCanonicalDecl(); 4716 if (cxx_method_decl) { 4717 name = cxx_method_decl->getDeclName().getAsString(); 4718 if (cxx_method_decl->isStatic()) 4719 kind = lldb::eMemberFunctionKindStaticMethod; 4720 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl)) 4721 kind = lldb::eMemberFunctionKindConstructor; 4722 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl)) 4723 kind = lldb::eMemberFunctionKindDestructor; 4724 else 4725 kind = lldb::eMemberFunctionKindInstanceMethod; 4726 clang_type = CompilerType( 4727 this, cxx_method_decl->getType().getAsOpaquePtr()); 4728 clang_decl = CompilerDecl(this, cxx_method_decl); 4729 } 4730 } 4731 } 4732 } 4733 break; 4734 4735 case clang::Type::ObjCObjectPointer: { 4736 const clang::ObjCObjectPointerType *objc_class_type = 4737 qual_type->getAs<clang::ObjCObjectPointerType>(); 4738 const clang::ObjCInterfaceType *objc_interface_type = 4739 objc_class_type->getInterfaceType(); 4740 if (objc_interface_type && 4741 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( 4742 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { 4743 clang::ObjCInterfaceDecl *class_interface_decl = 4744 objc_interface_type->getDecl(); 4745 if (class_interface_decl) { 4746 auto method_iter = class_interface_decl->meth_begin(); 4747 auto method_end = class_interface_decl->meth_end(); 4748 if (idx < 4749 static_cast<size_t>(std::distance(method_iter, method_end))) { 4750 std::advance(method_iter, idx); 4751 clang::ObjCMethodDecl *objc_method_decl = 4752 method_iter->getCanonicalDecl(); 4753 if (objc_method_decl) { 4754 clang_decl = CompilerDecl(this, objc_method_decl); 4755 name = objc_method_decl->getSelector().getAsString(); 4756 if (objc_method_decl->isClassMethod()) 4757 kind = lldb::eMemberFunctionKindStaticMethod; 4758 else 4759 kind = lldb::eMemberFunctionKindInstanceMethod; 4760 } 4761 } 4762 } 4763 } 4764 break; 4765 } 4766 4767 case clang::Type::ObjCObject: 4768 case clang::Type::ObjCInterface: 4769 if (GetCompleteType(type)) { 4770 const clang::ObjCObjectType *objc_class_type = 4771 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); 4772 if (objc_class_type) { 4773 clang::ObjCInterfaceDecl *class_interface_decl = 4774 objc_class_type->getInterface(); 4775 if (class_interface_decl) { 4776 auto method_iter = class_interface_decl->meth_begin(); 4777 auto method_end = class_interface_decl->meth_end(); 4778 if (idx < 4779 static_cast<size_t>(std::distance(method_iter, method_end))) { 4780 std::advance(method_iter, idx); 4781 clang::ObjCMethodDecl *objc_method_decl = 4782 method_iter->getCanonicalDecl(); 4783 if (objc_method_decl) { 4784 clang_decl = CompilerDecl(this, objc_method_decl); 4785 name = objc_method_decl->getSelector().getAsString(); 4786 if (objc_method_decl->isClassMethod()) 4787 kind = lldb::eMemberFunctionKindStaticMethod; 4788 else 4789 kind = lldb::eMemberFunctionKindInstanceMethod; 4790 } 4791 } 4792 } 4793 } 4794 } 4795 break; 4796 4797 case clang::Type::Typedef: 4798 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type) 4799 ->getDecl() 4800 ->getUnderlyingType() 4801 .getAsOpaquePtr(), 4802 idx); 4803 4804 case clang::Type::Auto: 4805 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type) 4806 ->getDeducedType() 4807 .getAsOpaquePtr(), 4808 idx); 4809 4810 case clang::Type::Elaborated: 4811 return GetMemberFunctionAtIndex( 4812 llvm::cast<clang::ElaboratedType>(qual_type) 4813 ->getNamedType() 4814 .getAsOpaquePtr(), 4815 idx); 4816 4817 case clang::Type::Paren: 4818 return GetMemberFunctionAtIndex( 4819 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), 4820 idx); 4821 4822 default: 4823 break; 4824 } 4825 } 4826 4827 if (kind == eMemberFunctionKindUnknown) 4828 return TypeMemberFunctionImpl(); 4829 else 4830 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind); 4831} 4832 4833CompilerType 4834ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) { 4835 if (type) 4836 return CompilerType(getASTContext(), 4837 GetQualType(type).getNonReferenceType()); 4838 return CompilerType(); 4839} 4840 4841CompilerType ClangASTContext::CreateTypedefType( 4842 const CompilerType &type, const char *typedef_name, 4843 const CompilerDeclContext &compiler_decl_ctx) { 4844 if (type && typedef_name && typedef_name[0]) { 4845 ClangASTContext *ast = 4846 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); 4847 if (!ast) 4848 return CompilerType(); 4849 clang::ASTContext *clang_ast = ast->getASTContext(); 4850 clang::QualType qual_type(ClangUtil::GetQualType(type)); 4851 4852 clang::DeclContext *decl_ctx = 4853 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx); 4854 if (decl_ctx == nullptr) 4855 decl_ctx = ast->getASTContext()->getTranslationUnitDecl(); 4856 4857 clang::TypedefDecl *decl = clang::TypedefDecl::Create( 4858 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(), 4859 &clang_ast->Idents.get(typedef_name), 4860 clang_ast->getTrivialTypeSourceInfo(qual_type)); 4861 4862 decl->setAccess(clang::AS_public); // TODO respect proper access specifier 4863 4864 decl_ctx->addDecl(decl); 4865 4866 // Get a uniqued clang::QualType for the typedef decl type 4867 return CompilerType(clang_ast, clang_ast->getTypedefType(decl)); 4868 } 4869 return CompilerType(); 4870} 4871 4872CompilerType 4873ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) { 4874 if (type) { 4875 clang::QualType qual_type(GetQualType(type)); 4876 return CompilerType(getASTContext(), 4877 qual_type.getTypePtr()->getPointeeType()); 4878 } 4879 return CompilerType(); 4880} 4881 4882CompilerType 4883ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) { 4884 if (type) { 4885 clang::QualType qual_type(GetQualType(type)); 4886 4887 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4888 switch (type_class) { 4889 case clang::Type::ObjCObject: 4890 case clang::Type::ObjCInterface: 4891 return CompilerType(getASTContext(), 4892 getASTContext()->getObjCObjectPointerType(qual_type)); 4893 4894 default: 4895 return CompilerType(getASTContext(), 4896 getASTContext()->getPointerType(qual_type)); 4897 } 4898 } 4899 return CompilerType(); 4900} 4901 4902CompilerType 4903ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) { 4904 if (type) 4905 return CompilerType(this, getASTContext() 4906 ->getLValueReferenceType(GetQualType(type)) 4907 .getAsOpaquePtr()); 4908 else 4909 return CompilerType(); 4910} 4911 4912CompilerType 4913ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) { 4914 if (type) 4915 return CompilerType(this, getASTContext() 4916 ->getRValueReferenceType(GetQualType(type)) 4917 .getAsOpaquePtr()); 4918 else 4919 return CompilerType(); 4920} 4921 4922CompilerType 4923ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) { 4924 if (type) { 4925 clang::QualType result(GetQualType(type)); 4926 result.addConst(); 4927 return CompilerType(this, result.getAsOpaquePtr()); 4928 } 4929 return CompilerType(); 4930} 4931 4932CompilerType 4933ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) { 4934 if (type) { 4935 clang::QualType result(GetQualType(type)); 4936 result.addVolatile(); 4937 return CompilerType(this, result.getAsOpaquePtr()); 4938 } 4939 return CompilerType(); 4940} 4941 4942CompilerType 4943ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) { 4944 if (type) { 4945 clang::QualType result(GetQualType(type)); 4946 result.addRestrict(); 4947 return CompilerType(this, result.getAsOpaquePtr()); 4948 } 4949 return CompilerType(); 4950} 4951 4952CompilerType 4953ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type, 4954 const char *typedef_name, 4955 const CompilerDeclContext &compiler_decl_ctx) { 4956 if (type) { 4957 clang::ASTContext *clang_ast = getASTContext(); 4958 clang::QualType qual_type(GetQualType(type)); 4959 4960 clang::DeclContext *decl_ctx = 4961 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx); 4962 if (decl_ctx == nullptr) 4963 decl_ctx = getASTContext()->getTranslationUnitDecl(); 4964 4965 clang::TypedefDecl *decl = clang::TypedefDecl::Create( 4966 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(), 4967 &clang_ast->Idents.get(typedef_name), 4968 clang_ast->getTrivialTypeSourceInfo(qual_type)); 4969 4970 clang::TagDecl *tdecl = nullptr; 4971 if (!qual_type.isNull()) { 4972 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>()) 4973 tdecl = rt->getDecl(); 4974 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>()) 4975 tdecl = et->getDecl(); 4976 } 4977 4978 // Check whether this declaration is an anonymous struct, union, or enum, 4979 // hidden behind a typedef. If so, we try to check whether we have a 4980 // typedef tag to attach to the original record declaration 4981 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl()) 4982 tdecl->setTypedefNameForAnonDecl(decl); 4983 4984 decl->setAccess(clang::AS_public); // TODO respect proper access specifier 4985 4986 // Get a uniqued clang::QualType for the typedef decl type 4987 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr()); 4988 } 4989 return CompilerType(); 4990} 4991 4992CompilerType 4993ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) { 4994 if (type) { 4995 const clang::TypedefType *typedef_type = 4996 llvm::dyn_cast<clang::TypedefType>(GetQualType(type)); 4997 if (typedef_type) 4998 return CompilerType(getASTContext(), 4999 typedef_type->getDecl()->getUnderlyingType()); 5000 } 5001 return CompilerType(); 5002} 5003 5004//---------------------------------------------------------------------- 5005// Create related types using the current type's AST 5006//---------------------------------------------------------------------- 5007 5008CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) { 5009 return ClangASTContext::GetBasicType(getASTContext(), basic_type); 5010} 5011//---------------------------------------------------------------------- 5012// Exploring the type 5013//---------------------------------------------------------------------- 5014 5015uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type, 5016 ExecutionContextScope *exe_scope) { 5017 if (GetCompleteType(type)) { 5018 clang::QualType qual_type(GetCanonicalQualType(type)); 5019 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5020 switch (type_class) { 5021 case clang::Type::Record: 5022 if (GetCompleteType(type)) 5023 return getASTContext()->getTypeSize(qual_type); 5024 else 5025 return 0; 5026 break; 5027 5028 case clang::Type::ObjCInterface: 5029 case clang::Type::ObjCObject: { 5030 ExecutionContext exe_ctx(exe_scope); 5031 Process *process = exe_ctx.GetProcessPtr(); 5032 if (process) { 5033 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime(); 5034 if (objc_runtime) { 5035 uint64_t bit_size = 0; 5036 if (objc_runtime->GetTypeBitSize( 5037 CompilerType(getASTContext(), qual_type), bit_size)) 5038 return bit_size; 5039 } 5040 } else { 5041 static bool g_printed = false; 5042 if (!g_printed) { 5043 StreamString s; 5044 DumpTypeDescription(type, &s); 5045 5046 llvm::outs() << "warning: trying to determine the size of type "; 5047 llvm::outs() << s.GetString() << "\n"; 5048 llvm::outs() << "without a valid ExecutionContext. this is not " 5049 "reliable. please file a bug against LLDB.\n"; 5050 llvm::outs() << "backtrace:\n"; 5051 llvm::sys::PrintStackTrace(llvm::outs()); 5052 llvm::outs() << "\n"; 5053 g_printed = true; 5054 } 5055 } 5056 } 5057 LLVM_FALLTHROUGH; 5058 default: 5059 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type); 5060 if (bit_size == 0) { 5061 if (qual_type->isIncompleteArrayType()) 5062 return getASTContext()->getTypeSize( 5063 qual_type->getArrayElementTypeNoTypeQual() 5064 ->getCanonicalTypeUnqualified()); 5065 } 5066 if (qual_type->isObjCObjectOrInterfaceType()) 5067 return bit_size + 5068 getASTContext()->getTypeSize( 5069 getASTContext()->ObjCBuiltinClassTy); 5070 return bit_size; 5071 } 5072 } 5073 return 0; 5074} 5075 5076size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) { 5077 if (GetCompleteType(type)) 5078 return getASTContext()->getTypeAlign(GetQualType(type)); 5079 return 0; 5080} 5081 5082lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type, 5083 uint64_t &count) { 5084 if (!type) 5085 return lldb::eEncodingInvalid; 5086 5087 count = 1; 5088 clang::QualType qual_type(GetCanonicalQualType(type)); 5089 5090 switch (qual_type->getTypeClass()) { 5091 case clang::Type::UnaryTransform: 5092 break; 5093 5094 case clang::Type::FunctionNoProto: 5095 case clang::Type::FunctionProto: 5096 break; 5097 5098 case clang::Type::IncompleteArray: 5099 case clang::Type::VariableArray: 5100 break; 5101 5102 case clang::Type::ConstantArray: 5103 break; 5104 5105 case clang::Type::DependentVector: 5106 case clang::Type::ExtVector: 5107 case clang::Type::Vector: 5108 // TODO: Set this to more than one??? 5109 break; 5110 5111 case clang::Type::Builtin: 5112 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { 5113 case clang::BuiltinType::Void: 5114 break; 5115 5116 case clang::BuiltinType::Bool: 5117 case clang::BuiltinType::Char_S: 5118 case clang::BuiltinType::SChar: 5119 case clang::BuiltinType::WChar_S: 5120 case clang::BuiltinType::Short: 5121 case clang::BuiltinType::Int: 5122 case clang::BuiltinType::Long: 5123 case clang::BuiltinType::LongLong: 5124 case clang::BuiltinType::Int128: 5125 return lldb::eEncodingSint; 5126 5127 case clang::BuiltinType::Char_U: 5128 case clang::BuiltinType::UChar: 5129 case clang::BuiltinType::WChar_U: 5130 case clang::BuiltinType::Char8: 5131 case clang::BuiltinType::Char16: 5132 case clang::BuiltinType::Char32: 5133 case clang::BuiltinType::UShort: 5134 case clang::BuiltinType::UInt: 5135 case clang::BuiltinType::ULong: 5136 case clang::BuiltinType::ULongLong: 5137 case clang::BuiltinType::UInt128: 5138 return lldb::eEncodingUint; 5139 5140 // Fixed point types. Note that they are currently ignored. 5141 case clang::BuiltinType::ShortAccum: 5142 case clang::BuiltinType::Accum: 5143 case clang::BuiltinType::LongAccum: 5144 case clang::BuiltinType::UShortAccum: 5145 case clang::BuiltinType::UAccum: 5146 case clang::BuiltinType::ULongAccum: 5147 case clang::BuiltinType::ShortFract: 5148 case clang::BuiltinType::Fract: 5149 case clang::BuiltinType::LongFract: 5150 case clang::BuiltinType::UShortFract: 5151 case clang::BuiltinType::UFract: 5152 case clang::BuiltinType::ULongFract: 5153 case clang::BuiltinType::SatShortAccum: 5154 case clang::BuiltinType::SatAccum: 5155 case clang::BuiltinType::SatLongAccum: 5156 case clang::BuiltinType::SatUShortAccum: 5157 case clang::BuiltinType::SatUAccum: 5158 case clang::BuiltinType::SatULongAccum: 5159 case clang::BuiltinType::SatShortFract: 5160 case clang::BuiltinType::SatFract: 5161 case clang::BuiltinType::SatLongFract: 5162 case clang::BuiltinType::SatUShortFract: 5163 case clang::BuiltinType::SatUFract: 5164 case clang::BuiltinType::SatULongFract: 5165 break; 5166 5167 case clang::BuiltinType::Half: 5168 case clang::BuiltinType::Float: 5169 case clang::BuiltinType::Float16: 5170 case clang::BuiltinType::Float128: 5171 case clang::BuiltinType::Double: 5172 case clang::BuiltinType::LongDouble: 5173 return lldb::eEncodingIEEE754; 5174 5175 case clang::BuiltinType::ObjCClass: 5176 case clang::BuiltinType::ObjCId: 5177 case clang::BuiltinType::ObjCSel: 5178 return lldb::eEncodingUint; 5179 5180 case clang::BuiltinType::NullPtr: 5181 return lldb::eEncodingUint; 5182 5183 case clang::BuiltinType::Kind::ARCUnbridgedCast: 5184 case clang::BuiltinType::Kind::BoundMember: 5185 case clang::BuiltinType::Kind::BuiltinFn: 5186 case clang::BuiltinType::Kind::Dependent: 5187 case clang::BuiltinType::Kind::OCLClkEvent: 5188 case clang::BuiltinType::Kind::OCLEvent: 5189 case clang::BuiltinType::Kind::OCLImage1dRO: 5190 case clang::BuiltinType::Kind::OCLImage1dWO: 5191 case clang::BuiltinType::Kind::OCLImage1dRW: 5192 case clang::BuiltinType::Kind::OCLImage1dArrayRO: 5193 case clang::BuiltinType::Kind::OCLImage1dArrayWO: 5194 case clang::BuiltinType::Kind::OCLImage1dArrayRW: 5195 case clang::BuiltinType::Kind::OCLImage1dBufferRO: 5196 case clang::BuiltinType::Kind::OCLImage1dBufferWO: 5197 case clang::BuiltinType::Kind::OCLImage1dBufferRW: 5198 case clang::BuiltinType::Kind::OCLImage2dRO: 5199 case clang::BuiltinType::Kind::OCLImage2dWO: 5200 case clang::BuiltinType::Kind::OCLImage2dRW: 5201 case clang::BuiltinType::Kind::OCLImage2dArrayRO: 5202 case clang::BuiltinType::Kind::OCLImage2dArrayWO: 5203 case clang::BuiltinType::Kind::OCLImage2dArrayRW: 5204 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO: 5205 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO: 5206 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW: 5207 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO: 5208 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO: 5209 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW: 5210 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO: 5211 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO: 5212 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW: 5213 case clang::BuiltinType::Kind::OCLImage2dDepthRO: 5214 case clang::BuiltinType::Kind::OCLImage2dDepthWO: 5215 case clang::BuiltinType::Kind::OCLImage2dDepthRW: 5216 case clang::BuiltinType::Kind::OCLImage2dMSAARO: 5217 case clang::BuiltinType::Kind::OCLImage2dMSAAWO: 5218 case clang::BuiltinType::Kind::OCLImage2dMSAARW: 5219 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO: 5220 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO: 5221 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW: 5222 case clang::BuiltinType::Kind::OCLImage3dRO: 5223 case clang::BuiltinType::Kind::OCLImage3dWO: 5224 case clang::BuiltinType::Kind::OCLImage3dRW: 5225 case clang::BuiltinType::Kind::OCLQueue: 5226 case clang::BuiltinType::Kind::OCLReserveID: 5227 case clang::BuiltinType::Kind::OCLSampler: 5228 case clang::BuiltinType::Kind::OMPArraySection: 5229 case clang::BuiltinType::Kind::Overload: 5230 case clang::BuiltinType::Kind::PseudoObject: 5231 case clang::BuiltinType::Kind::UnknownAny: 5232 break; 5233 5234 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload: 5235 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload: 5236 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload: 5237 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload: 5238 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult: 5239 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult: 5240 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult: 5241 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult: 5242 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout: 5243 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout: 5244 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin: 5245 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin: 5246 break; 5247 } 5248 break; 5249 // All pointer types are represented as unsigned integer encodings. We may 5250 // nee to add a eEncodingPointer if we ever need to know the difference 5251 case clang::Type::ObjCObjectPointer: 5252 case clang::Type::BlockPointer: 5253 case clang::Type::Pointer: 5254 case clang::Type::LValueReference: 5255 case clang::Type::RValueReference: 5256 case clang::Type::MemberPointer: 5257 return lldb::eEncodingUint; 5258 case clang::Type::Complex: { 5259 lldb::Encoding encoding = lldb::eEncodingIEEE754; 5260 if (qual_type->isComplexType()) 5261 encoding = lldb::eEncodingIEEE754; 5262 else { 5263 const clang::ComplexType *complex_type = 5264 qual_type->getAsComplexIntegerType(); 5265 if (complex_type) 5266 encoding = CompilerType(getASTContext(), complex_type->getElementType()) 5267 .GetEncoding(count); 5268 else 5269 encoding = lldb::eEncodingSint; 5270 } 5271 count = 2; 5272 return encoding; 5273 } 5274 5275 case clang::Type::ObjCInterface: 5276 break; 5277 case clang::Type::Record: 5278 break; 5279 case clang::Type::Enum: 5280 return lldb::eEncodingSint; 5281 case clang::Type::Typedef: 5282 return CompilerType(getASTContext(), 5283 llvm::cast<clang::TypedefType>(qual_type) 5284 ->getDecl() 5285 ->getUnderlyingType()) 5286 .GetEncoding(count); 5287 5288 case clang::Type::Auto: 5289 return CompilerType( 5290 getASTContext(), 5291 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) 5292 .GetEncoding(count); 5293 5294 case clang::Type::Elaborated: 5295 return CompilerType( 5296 getASTContext(), 5297 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) 5298 .GetEncoding(count); 5299 5300 case clang::Type::Paren: 5301 return CompilerType(getASTContext(), 5302 llvm::cast<clang::ParenType>(qual_type)->desugar()) 5303 .GetEncoding(count); 5304 case clang::Type::TypeOfExpr: 5305 return CompilerType(getASTContext(), 5306 llvm::cast<clang::TypeOfExprType>(qual_type) 5307 ->getUnderlyingExpr() 5308 ->getType()) 5309 .GetEncoding(count); 5310 case clang::Type::TypeOf: 5311 return CompilerType( 5312 getASTContext(), 5313 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) 5314 .GetEncoding(count); 5315 case clang::Type::Decltype: 5316 return CompilerType( 5317 getASTContext(), 5318 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()) 5319 .GetEncoding(count); 5320 case clang::Type::DependentSizedArray: 5321 case clang::Type::DependentSizedExtVector: 5322 case clang::Type::UnresolvedUsing: 5323 case clang::Type::Attributed: 5324 case clang::Type::TemplateTypeParm: 5325 case clang::Type::SubstTemplateTypeParm: 5326 case clang::Type::SubstTemplateTypeParmPack: 5327 case clang::Type::InjectedClassName: 5328 case clang::Type::DependentName: 5329 case clang::Type::DependentTemplateSpecialization: 5330 case clang::Type::PackExpansion: 5331 case clang::Type::ObjCObject: 5332 5333 case clang::Type::TemplateSpecialization: 5334 case clang::Type::DeducedTemplateSpecialization: 5335 case clang::Type::Atomic: 5336 case clang::Type::Adjusted: 5337 case clang::Type::Pipe: 5338 break; 5339 5340 // pointer type decayed from an array or function type. 5341 case clang::Type::Decayed: 5342 break; 5343 case clang::Type::ObjCTypeParam: 5344 break; 5345 5346 case clang::Type::DependentAddressSpace: 5347 break; 5348 } 5349 count = 0; 5350 return lldb::eEncodingInvalid; 5351} 5352 5353lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) { 5354 if (!type) 5355 return lldb::eFormatDefault; 5356 5357 clang::QualType qual_type(GetCanonicalQualType(type)); 5358 5359 switch (qual_type->getTypeClass()) { 5360 case clang::Type::UnaryTransform: 5361 break; 5362 5363 case clang::Type::FunctionNoProto: 5364 case clang::Type::FunctionProto: 5365 break; 5366 5367 case clang::Type::IncompleteArray: 5368 case clang::Type::VariableArray: 5369 break; 5370 5371 case clang::Type::ConstantArray: 5372 return lldb::eFormatVoid; // no value 5373 5374 case clang::Type::DependentVector: 5375 case clang::Type::ExtVector: 5376 case clang::Type::Vector: 5377 break; 5378 5379 case clang::Type::Builtin: 5380 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { 5381 // default: assert(0 && "Unknown builtin type!"); 5382 case clang::BuiltinType::UnknownAny: 5383 case clang::BuiltinType::Void: 5384 case clang::BuiltinType::BoundMember: 5385 break; 5386 5387 case clang::BuiltinType::Bool: 5388 return lldb::eFormatBoolean; 5389 case clang::BuiltinType::Char_S: 5390 case clang::BuiltinType::SChar: 5391 case clang::BuiltinType::WChar_S: 5392 case clang::BuiltinType::Char_U: 5393 case clang::BuiltinType::UChar: 5394 case clang::BuiltinType::WChar_U: 5395 return lldb::eFormatChar; 5396 case clang::BuiltinType::Char16: 5397 return lldb::eFormatUnicode16; 5398 case clang::BuiltinType::Char32: 5399 return lldb::eFormatUnicode32; 5400 case clang::BuiltinType::UShort: 5401 return lldb::eFormatUnsigned; 5402 case clang::BuiltinType::Short: 5403 return lldb::eFormatDecimal; 5404 case clang::BuiltinType::UInt: 5405 return lldb::eFormatUnsigned; 5406 case clang::BuiltinType::Int: 5407 return lldb::eFormatDecimal; 5408 case clang::BuiltinType::ULong: 5409 return lldb::eFormatUnsigned; 5410 case clang::BuiltinType::Long: 5411 return lldb::eFormatDecimal; 5412 case clang::BuiltinType::ULongLong: 5413 return lldb::eFormatUnsigned; 5414 case clang::BuiltinType::LongLong: 5415 return lldb::eFormatDecimal; 5416 case clang::BuiltinType::UInt128: 5417 return lldb::eFormatUnsigned; 5418 case clang::BuiltinType::Int128: 5419 return lldb::eFormatDecimal; 5420 case clang::BuiltinType::Half: 5421 case clang::BuiltinType::Float: 5422 case clang::BuiltinType::Double: 5423 case clang::BuiltinType::LongDouble: 5424 return lldb::eFormatFloat; 5425 default: 5426 return lldb::eFormatHex; 5427 } 5428 break; 5429 case clang::Type::ObjCObjectPointer: 5430 return lldb::eFormatHex; 5431 case clang::Type::BlockPointer: 5432 return lldb::eFormatHex; 5433 case clang::Type::Pointer: 5434 return lldb::eFormatHex; 5435 case clang::Type::LValueReference: 5436 case clang::Type::RValueReference: 5437 return lldb::eFormatHex; 5438 case clang::Type::MemberPointer: 5439 break; 5440 case clang::Type::Complex: { 5441 if (qual_type->isComplexType()) 5442 return lldb::eFormatComplex; 5443 else 5444 return lldb::eFormatComplexInteger; 5445 } 5446 case clang::Type::ObjCInterface: 5447 break; 5448 case clang::Type::Record: 5449 break; 5450 case clang::Type::Enum: 5451 return lldb::eFormatEnum; 5452 case clang::Type::Typedef: 5453 return CompilerType(getASTContext(), 5454 llvm::cast<clang::TypedefType>(qual_type) 5455 ->getDecl() 5456 ->getUnderlyingType()) 5457 .GetFormat(); 5458 case clang::Type::Auto: 5459 return CompilerType(getASTContext(), 5460 llvm::cast<clang::AutoType>(qual_type)->desugar()) 5461 .GetFormat(); 5462 case clang::Type::Paren: 5463 return CompilerType(getASTContext(), 5464 llvm::cast<clang::ParenType>(qual_type)->desugar()) 5465 .GetFormat(); 5466 case clang::Type::Elaborated: 5467 return CompilerType( 5468 getASTContext(), 5469 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) 5470 .GetFormat(); 5471 case clang::Type::TypeOfExpr: 5472 return CompilerType(getASTContext(), 5473 llvm::cast<clang::TypeOfExprType>(qual_type) 5474 ->getUnderlyingExpr() 5475 ->getType()) 5476 .GetFormat(); 5477 case clang::Type::TypeOf: 5478 return CompilerType( 5479 getASTContext(), 5480 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) 5481 .GetFormat(); 5482 case clang::Type::Decltype: 5483 return CompilerType( 5484 getASTContext(), 5485 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()) 5486 .GetFormat(); 5487 case clang::Type::DependentSizedArray: 5488 case clang::Type::DependentSizedExtVector: 5489 case clang::Type::UnresolvedUsing: 5490 case clang::Type::Attributed: 5491 case clang::Type::TemplateTypeParm: 5492 case clang::Type::SubstTemplateTypeParm: 5493 case clang::Type::SubstTemplateTypeParmPack: 5494 case clang::Type::InjectedClassName: 5495 case clang::Type::DependentName: 5496 case clang::Type::DependentTemplateSpecialization: 5497 case clang::Type::PackExpansion: 5498 case clang::Type::ObjCObject: 5499 5500 case clang::Type::TemplateSpecialization: 5501 case clang::Type::DeducedTemplateSpecialization: 5502 case clang::Type::Atomic: 5503 case clang::Type::Adjusted: 5504 case clang::Type::Pipe: 5505 break; 5506 5507 // pointer type decayed from an array or function type. 5508 case clang::Type::Decayed: 5509 break; 5510 case clang::Type::ObjCTypeParam: 5511 break; 5512 5513 case clang::Type::DependentAddressSpace: 5514 break; 5515 } 5516 // We don't know hot to display this type... 5517 return lldb::eFormatBytes; 5518} 5519 5520static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl, 5521 bool check_superclass) { 5522 while (class_interface_decl) { 5523 if (class_interface_decl->ivar_size() > 0) 5524 return true; 5525 5526 if (check_superclass) 5527 class_interface_decl = class_interface_decl->getSuperClass(); 5528 else 5529 break; 5530 } 5531 return false; 5532} 5533 5534static Optional<SymbolFile::ArrayInfo> 5535GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file, 5536 clang::QualType qual_type, 5537 const ExecutionContext *exe_ctx) { 5538 if (qual_type->isIncompleteArrayType()) 5539 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr())) 5540 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(), 5541 exe_ctx); 5542 return llvm::None; 5543} 5544 5545uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type, 5546 bool omit_empty_base_classes, 5547 const ExecutionContext *exe_ctx) { 5548 if (!type) 5549 return 0; 5550 5551 uint32_t num_children = 0; 5552 clang::QualType qual_type(GetQualType(type)); 5553 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5554 switch (type_class) { 5555 case clang::Type::Builtin: 5556 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { 5557 case clang::BuiltinType::ObjCId: // child is Class 5558 case clang::BuiltinType::ObjCClass: // child is Class 5559 num_children = 1; 5560 break; 5561 5562 default: 5563 break; 5564 } 5565 break; 5566 5567 case clang::Type::Complex: 5568 return 0; 5569 case clang::Type::Record: 5570 if (GetCompleteQualType(getASTContext(), qual_type)) { 5571 const clang::RecordType *record_type = 5572 llvm::cast<clang::RecordType>(qual_type.getTypePtr()); 5573 const clang::RecordDecl *record_decl = record_type->getDecl(); 5574 assert(record_decl); 5575 const clang::CXXRecordDecl *cxx_record_decl = 5576 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); 5577 if (cxx_record_decl) { 5578 if (omit_empty_base_classes) { 5579 // Check each base classes to see if it or any of its base classes 5580 // contain any fields. This can help limit the noise in variable 5581 // views by not having to show base classes that contain no members. 5582 clang::CXXRecordDecl::base_class_const_iterator base_class, 5583 base_class_end; 5584 for (base_class = cxx_record_decl->bases_begin(), 5585 base_class_end = cxx_record_decl->bases_end(); 5586 base_class != base_class_end; ++base_class) { 5587 const clang::CXXRecordDecl *base_class_decl = 5588 llvm::cast<clang::CXXRecordDecl>( 5589 base_class->getType() 5590 ->getAs<clang::RecordType>() 5591 ->getDecl()); 5592 5593 // Skip empty base classes 5594 if (!ClangASTContext::RecordHasFields(base_class_decl)) 5595 continue; 5596 5597 num_children++; 5598 } 5599 } else { 5600 // Include all base classes 5601 num_children += cxx_record_decl->getNumBases(); 5602 } 5603 } 5604 clang::RecordDecl::field_iterator field, field_end; 5605 for (field = record_decl->field_begin(), 5606 field_end = record_decl->field_end(); 5607 field != field_end; ++field) 5608 ++num_children; 5609 } 5610 break; 5611 5612 case clang::Type::ObjCObject: 5613 case clang::Type::ObjCInterface: 5614 if (GetCompleteQualType(getASTContext(), qual_type)) { 5615 const clang::ObjCObjectType *objc_class_type = 5616 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); 5617 assert(objc_class_type); 5618 if (objc_class_type) { 5619 clang::ObjCInterfaceDecl *class_interface_decl = 5620 objc_class_type->getInterface(); 5621 5622 if (class_interface_decl) { 5623 5624 clang::ObjCInterfaceDecl *superclass_interface_decl = 5625 class_interface_decl->getSuperClass(); 5626 if (superclass_interface_decl) { 5627 if (omit_empty_base_classes) { 5628 if (ObjCDeclHasIVars(superclass_interface_decl, true)) 5629 ++num_children; 5630 } else 5631 ++num_children; 5632 } 5633 5634 num_children += class_interface_decl->ivar_size(); 5635 } 5636 } 5637 } 5638 break; 5639 5640 case clang::Type::ObjCObjectPointer: { 5641 const clang::ObjCObjectPointerType *pointer_type = 5642 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()); 5643 clang::QualType pointee_type = pointer_type->getPointeeType(); 5644 uint32_t num_pointee_children = 5645 CompilerType(getASTContext(), pointee_type) 5646 .GetNumChildren(omit_empty_base_classes, exe_ctx); 5647 // If this type points to a simple type, then it has 1 child 5648 if (num_pointee_children == 0) 5649 num_children = 1; 5650 else 5651 num_children = num_pointee_children; 5652 } break; 5653 5654 case clang::Type::Vector: 5655 case clang::Type::ExtVector: 5656 num_children = 5657 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements(); 5658 break; 5659 5660 case clang::Type::ConstantArray: 5661 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr()) 5662 ->getSize() 5663 .getLimitedValue(); 5664 break; 5665 case clang::Type::IncompleteArray: 5666 if (auto array_info = 5667 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx)) 5668 // Only 1-dimensional arrays are supported. 5669 num_children = array_info->element_orders.size() 5670 ? array_info->element_orders.back() 5671 : 0; 5672 break; 5673 5674 case clang::Type::Pointer: { 5675 const clang::PointerType *pointer_type = 5676 llvm::cast<clang::PointerType>(qual_type.getTypePtr()); 5677 clang::QualType pointee_type(pointer_type->getPointeeType()); 5678 uint32_t num_pointee_children = 5679 CompilerType(getASTContext(), pointee_type) 5680 .GetNumChildren(omit_empty_base_classes, exe_ctx); 5681 if (num_pointee_children == 0) { 5682 // We have a pointer to a pointee type that claims it has no children. We 5683 // will want to look at 5684 num_children = GetNumPointeeChildren(pointee_type); 5685 } else 5686 num_children = num_pointee_children; 5687 } break; 5688 5689 case clang::Type::LValueReference: 5690 case clang::Type::RValueReference: { 5691 const clang::ReferenceType *reference_type = 5692 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); 5693 clang::QualType pointee_type = reference_type->getPointeeType(); 5694 uint32_t num_pointee_children = 5695 CompilerType(getASTContext(), pointee_type) 5696 .GetNumChildren(omit_empty_base_classes, exe_ctx); 5697 // If this type points to a simple type, then it has 1 child 5698 if (num_pointee_children == 0) 5699 num_children = 1; 5700 else 5701 num_children = num_pointee_children; 5702 } break; 5703 5704 case clang::Type::Typedef: 5705 num_children = 5706 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type) 5707 ->getDecl() 5708 ->getUnderlyingType()) 5709 .GetNumChildren(omit_empty_base_classes, exe_ctx); 5710 break; 5711 5712 case clang::Type::Auto: 5713 num_children = 5714 CompilerType(getASTContext(), 5715 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) 5716 .GetNumChildren(omit_empty_base_classes, exe_ctx); 5717 break; 5718 5719 case clang::Type::Elaborated: 5720 num_children = 5721 CompilerType( 5722 getASTContext(), 5723 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) 5724 .GetNumChildren(omit_empty_base_classes, exe_ctx); 5725 break; 5726 5727 case clang::Type::Paren: 5728 num_children = 5729 CompilerType(getASTContext(), 5730 llvm::cast<clang::ParenType>(qual_type)->desugar()) 5731 .GetNumChildren(omit_empty_base_classes, exe_ctx); 5732 break; 5733 default: 5734 break; 5735 } 5736 return num_children; 5737} 5738 5739CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) { 5740 return GetBasicType(GetBasicTypeEnumeration(name)); 5741} 5742 5743lldb::BasicType 5744ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) { 5745 if (type) { 5746 clang::QualType qual_type(GetQualType(type)); 5747 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5748 if (type_class == clang::Type::Builtin) { 5749 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { 5750 case clang::BuiltinType::Void: 5751 return eBasicTypeVoid; 5752 case clang::BuiltinType::Bool: 5753 return eBasicTypeBool; 5754 case clang::BuiltinType::Char_S: 5755 return eBasicTypeSignedChar; 5756 case clang::BuiltinType::Char_U: 5757 return eBasicTypeUnsignedChar; 5758 case clang::BuiltinType::Char16: 5759 return eBasicTypeChar16; 5760 case clang::BuiltinType::Char32: 5761 return eBasicTypeChar32; 5762 case clang::BuiltinType::UChar: 5763 return eBasicTypeUnsignedChar; 5764 case clang::BuiltinType::SChar: 5765 return eBasicTypeSignedChar; 5766 case clang::BuiltinType::WChar_S: 5767 return eBasicTypeSignedWChar; 5768 case clang::BuiltinType::WChar_U: 5769 return eBasicTypeUnsignedWChar; 5770 case clang::BuiltinType::Short: 5771 return eBasicTypeShort; 5772 case clang::BuiltinType::UShort: 5773 return eBasicTypeUnsignedShort; 5774 case clang::BuiltinType::Int: 5775 return eBasicTypeInt; 5776 case clang::BuiltinType::UInt: 5777 return eBasicTypeUnsignedInt; 5778 case clang::BuiltinType::Long: 5779 return eBasicTypeLong; 5780 case clang::BuiltinType::ULong: 5781 return eBasicTypeUnsignedLong; 5782 case clang::BuiltinType::LongLong: 5783 return eBasicTypeLongLong; 5784 case clang::BuiltinType::ULongLong: 5785 return eBasicTypeUnsignedLongLong; 5786 case clang::BuiltinType::Int128: 5787 return eBasicTypeInt128; 5788 case clang::BuiltinType::UInt128: 5789 return eBasicTypeUnsignedInt128; 5790 5791 case clang::BuiltinType::Half: 5792 return eBasicTypeHalf; 5793 case clang::BuiltinType::Float: 5794 return eBasicTypeFloat; 5795 case clang::BuiltinType::Double: 5796 return eBasicTypeDouble; 5797 case clang::BuiltinType::LongDouble: 5798 return eBasicTypeLongDouble; 5799 5800 case clang::BuiltinType::NullPtr: 5801 return eBasicTypeNullPtr; 5802 case clang::BuiltinType::ObjCId: 5803 return eBasicTypeObjCID; 5804 case clang::BuiltinType::ObjCClass: 5805 return eBasicTypeObjCClass; 5806 case clang::BuiltinType::ObjCSel: 5807 return eBasicTypeObjCSel; 5808 default: 5809 return eBasicTypeOther; 5810 } 5811 } 5812 } 5813 return eBasicTypeInvalid; 5814} 5815 5816void ClangASTContext::ForEachEnumerator( 5817 lldb::opaque_compiler_type_t type, 5818 std::function<bool(const CompilerType &integer_type, 5819 const ConstString &name, 5820 const llvm::APSInt &value)> const &callback) { 5821 const clang::EnumType *enum_type = 5822 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type)); 5823 if (enum_type) { 5824 const clang::EnumDecl *enum_decl = enum_type->getDecl(); 5825 if (enum_decl) { 5826 CompilerType integer_type(this, 5827 enum_decl->getIntegerType().getAsOpaquePtr()); 5828 5829 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos; 5830 for (enum_pos = enum_decl->enumerator_begin(), 5831 enum_end_pos = enum_decl->enumerator_end(); 5832 enum_pos != enum_end_pos; ++enum_pos) { 5833 ConstString name(enum_pos->getNameAsString().c_str()); 5834 if (!callback(integer_type, name, enum_pos->getInitVal())) 5835 break; 5836 } 5837 } 5838 } 5839} 5840 5841#pragma mark Aggregate Types 5842 5843uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) { 5844 if (!type) 5845 return 0; 5846 5847 uint32_t count = 0; 5848 clang::QualType qual_type(GetCanonicalQualType(type)); 5849 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5850 switch (type_class) { 5851 case clang::Type::Record: 5852 if (GetCompleteType(type)) { 5853 const clang::RecordType *record_type = 5854 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr()); 5855 if (record_type) { 5856 clang::RecordDecl *record_decl = record_type->getDecl(); 5857 if (record_decl) { 5858 uint32_t field_idx = 0; 5859 clang::RecordDecl::field_iterator field, field_end; 5860 for (field = record_decl->field_begin(), 5861 field_end = record_decl->field_end(); 5862 field != field_end; ++field) 5863 ++field_idx; 5864 count = field_idx; 5865 } 5866 } 5867 } 5868 break; 5869 5870 case clang::Type::Typedef: 5871 count = 5872 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type) 5873 ->getDecl() 5874 ->getUnderlyingType()) 5875 .GetNumFields(); 5876 break; 5877 5878 case clang::Type::Auto: 5879 count = 5880 CompilerType(getASTContext(), 5881 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) 5882 .GetNumFields(); 5883 break; 5884 5885 case clang::Type::Elaborated: 5886 count = CompilerType( 5887 getASTContext(), 5888 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) 5889 .GetNumFields(); 5890 break; 5891 5892 case clang::Type::Paren: 5893 count = CompilerType(getASTContext(), 5894 llvm::cast<clang::ParenType>(qual_type)->desugar()) 5895 .GetNumFields(); 5896 break; 5897 5898 case clang::Type::ObjCObjectPointer: { 5899 const clang::ObjCObjectPointerType *objc_class_type = 5900 qual_type->getAs<clang::ObjCObjectPointerType>(); 5901 const clang::ObjCInterfaceType *objc_interface_type = 5902 objc_class_type->getInterfaceType(); 5903 if (objc_interface_type && 5904 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( 5905 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { 5906 clang::ObjCInterfaceDecl *class_interface_decl = 5907 objc_interface_type->getDecl(); 5908 if (class_interface_decl) { 5909 count = class_interface_decl->ivar_size(); 5910 } 5911 } 5912 break; 5913 } 5914 5915 case clang::Type::ObjCObject: 5916 case clang::Type::ObjCInterface: 5917 if (GetCompleteType(type)) { 5918 const clang::ObjCObjectType *objc_class_type = 5919 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); 5920 if (objc_class_type) { 5921 clang::ObjCInterfaceDecl *class_interface_decl = 5922 objc_class_type->getInterface(); 5923 5924 if (class_interface_decl) 5925 count = class_interface_decl->ivar_size(); 5926 } 5927 } 5928 break; 5929 5930 default: 5931 break; 5932 } 5933 return count; 5934} 5935 5936static lldb::opaque_compiler_type_t 5937GetObjCFieldAtIndex(clang::ASTContext *ast, 5938 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx, 5939 std::string &name, uint64_t *bit_offset_ptr, 5940 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) { 5941 if (class_interface_decl) { 5942 if (idx < (class_interface_decl->ivar_size())) { 5943 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, 5944 ivar_end = class_interface_decl->ivar_end(); 5945 uint32_t ivar_idx = 0; 5946 5947 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; 5948 ++ivar_pos, ++ivar_idx) { 5949 if (ivar_idx == idx) { 5950 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos; 5951 5952 clang::QualType ivar_qual_type(ivar_decl->getType()); 5953 5954 name.assign(ivar_decl->getNameAsString()); 5955 5956 if (bit_offset_ptr) { 5957 const clang::ASTRecordLayout &interface_layout = 5958 ast->getASTObjCInterfaceLayout(class_interface_decl); 5959 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx); 5960 } 5961 5962 const bool is_bitfield = ivar_pos->isBitField(); 5963 5964 if (bitfield_bit_size_ptr) { 5965 *bitfield_bit_size_ptr = 0; 5966 5967 if (is_bitfield && ast) { 5968 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth(); 5969 clang::Expr::EvalResult result; 5970 if (bitfield_bit_size_expr && 5971 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) { 5972 llvm::APSInt bitfield_apsint = result.Val.getInt(); 5973 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue(); 5974 } 5975 } 5976 } 5977 if (is_bitfield_ptr) 5978 *is_bitfield_ptr = is_bitfield; 5979 5980 return ivar_qual_type.getAsOpaquePtr(); 5981 } 5982 } 5983 } 5984 } 5985 return nullptr; 5986} 5987 5988CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type, 5989 size_t idx, std::string &name, 5990 uint64_t *bit_offset_ptr, 5991 uint32_t *bitfield_bit_size_ptr, 5992 bool *is_bitfield_ptr) { 5993 if (!type) 5994 return CompilerType(); 5995 5996 clang::QualType qual_type(GetCanonicalQualType(type)); 5997 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 5998 switch (type_class) { 5999 case clang::Type::Record: 6000 if (GetCompleteType(type)) { 6001 const clang::RecordType *record_type = 6002 llvm::cast<clang::RecordType>(qual_type.getTypePtr()); 6003 const clang::RecordDecl *record_decl = record_type->getDecl(); 6004 uint32_t field_idx = 0; 6005 clang::RecordDecl::field_iterator field, field_end; 6006 for (field = record_decl->field_begin(), 6007 field_end = record_decl->field_end(); 6008 field != field_end; ++field, ++field_idx) { 6009 if (idx == field_idx) { 6010 // Print the member type if requested 6011 // Print the member name and equal sign 6012 name.assign(field->getNameAsString()); 6013 6014 // Figure out the type byte size (field_type_info.first) and 6015 // alignment (field_type_info.second) from the AST context. 6016 if (bit_offset_ptr) { 6017 const clang::ASTRecordLayout &record_layout = 6018 getASTContext()->getASTRecordLayout(record_decl); 6019 *bit_offset_ptr = record_layout.getFieldOffset(field_idx); 6020 } 6021 6022 const bool is_bitfield = field->isBitField(); 6023 6024 if (bitfield_bit_size_ptr) { 6025 *bitfield_bit_size_ptr = 0; 6026 6027 if (is_bitfield) { 6028 clang::Expr *bitfield_bit_size_expr = field->getBitWidth(); 6029 clang::Expr::EvalResult result; 6030 if (bitfield_bit_size_expr && 6031 bitfield_bit_size_expr->EvaluateAsInt(result, 6032 *getASTContext())) { 6033 llvm::APSInt bitfield_apsint = result.Val.getInt(); 6034 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue(); 6035 } 6036 } 6037 } 6038 if (is_bitfield_ptr) 6039 *is_bitfield_ptr = is_bitfield; 6040 6041 return CompilerType(getASTContext(), field->getType()); 6042 } 6043 } 6044 } 6045 break; 6046 6047 case clang::Type::ObjCObjectPointer: { 6048 const clang::ObjCObjectPointerType *objc_class_type = 6049 qual_type->getAs<clang::ObjCObjectPointerType>(); 6050 const clang::ObjCInterfaceType *objc_interface_type = 6051 objc_class_type->getInterfaceType(); 6052 if (objc_interface_type && 6053 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( 6054 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { 6055 clang::ObjCInterfaceDecl *class_interface_decl = 6056 objc_interface_type->getDecl(); 6057 if (class_interface_decl) { 6058 return CompilerType( 6059 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl, 6060 idx, name, bit_offset_ptr, 6061 bitfield_bit_size_ptr, is_bitfield_ptr)); 6062 } 6063 } 6064 break; 6065 } 6066 6067 case clang::Type::ObjCObject: 6068 case clang::Type::ObjCInterface: 6069 if (GetCompleteType(type)) { 6070 const clang::ObjCObjectType *objc_class_type = 6071 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); 6072 assert(objc_class_type); 6073 if (objc_class_type) { 6074 clang::ObjCInterfaceDecl *class_interface_decl = 6075 objc_class_type->getInterface(); 6076 return CompilerType( 6077 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl, 6078 idx, name, bit_offset_ptr, 6079 bitfield_bit_size_ptr, is_bitfield_ptr)); 6080 } 6081 } 6082 break; 6083 6084 case clang::Type::Typedef: 6085 return CompilerType(getASTContext(), 6086 llvm::cast<clang::TypedefType>(qual_type) 6087 ->getDecl() 6088 ->getUnderlyingType()) 6089 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, 6090 is_bitfield_ptr); 6091 6092 case clang::Type::Auto: 6093 return CompilerType( 6094 getASTContext(), 6095 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) 6096 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, 6097 is_bitfield_ptr); 6098 6099 case clang::Type::Elaborated: 6100 return CompilerType( 6101 getASTContext(), 6102 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) 6103 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, 6104 is_bitfield_ptr); 6105 6106 case clang::Type::Paren: 6107 return CompilerType(getASTContext(), 6108 llvm::cast<clang::ParenType>(qual_type)->desugar()) 6109 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, 6110 is_bitfield_ptr); 6111 6112 default: 6113 break; 6114 } 6115 return CompilerType(); 6116} 6117 6118uint32_t 6119ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) { 6120 uint32_t count = 0; 6121 clang::QualType qual_type(GetCanonicalQualType(type)); 6122 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 6123 switch (type_class) { 6124 case clang::Type::Record: 6125 if (GetCompleteType(type)) { 6126 const clang::CXXRecordDecl *cxx_record_decl = 6127 qual_type->getAsCXXRecordDecl(); 6128 if (cxx_record_decl) 6129 count = cxx_record_decl->getNumBases(); 6130 } 6131 break; 6132 6133 case clang::Type::ObjCObjectPointer: 6134 count = GetPointeeType(type).GetNumDirectBaseClasses(); 6135 break; 6136 6137 case clang::Type::ObjCObject: 6138 if (GetCompleteType(type)) { 6139 const clang::ObjCObjectType *objc_class_type = 6140 qual_type->getAsObjCQualifiedInterfaceType(); 6141 if (objc_class_type) { 6142 clang::ObjCInterfaceDecl *class_interface_decl = 6143 objc_class_type->getInterface(); 6144 6145 if (class_interface_decl && class_interface_decl->getSuperClass()) 6146 count = 1; 6147 } 6148 } 6149 break; 6150 case clang::Type::ObjCInterface: 6151 if (GetCompleteType(type)) { 6152 const clang::ObjCInterfaceType *objc_interface_type = 6153 qual_type->getAs<clang::ObjCInterfaceType>(); 6154 if (objc_interface_type) { 6155 clang::ObjCInterfaceDecl *class_interface_decl = 6156 objc_interface_type->getInterface(); 6157 6158 if (class_interface_decl && class_interface_decl->getSuperClass()) 6159 count = 1; 6160 } 6161 } 6162 break; 6163 6164 case clang::Type::Typedef: 6165 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type) 6166 ->getDecl() 6167 ->getUnderlyingType() 6168 .getAsOpaquePtr()); 6169 break; 6170 6171 case clang::Type::Auto: 6172 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type) 6173 ->getDeducedType() 6174 .getAsOpaquePtr()); 6175 break; 6176 6177 case clang::Type::Elaborated: 6178 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type) 6179 ->getNamedType() 6180 .getAsOpaquePtr()); 6181 break; 6182 6183 case clang::Type::Paren: 6184 return GetNumDirectBaseClasses( 6185 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); 6186 6187 default: 6188 break; 6189 } 6190 return count; 6191} 6192 6193uint32_t 6194ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) { 6195 uint32_t count = 0; 6196 clang::QualType qual_type(GetCanonicalQualType(type)); 6197 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 6198 switch (type_class) { 6199 case clang::Type::Record: 6200 if (GetCompleteType(type)) { 6201 const clang::CXXRecordDecl *cxx_record_decl = 6202 qual_type->getAsCXXRecordDecl(); 6203 if (cxx_record_decl) 6204 count = cxx_record_decl->getNumVBases(); 6205 } 6206 break; 6207 6208 case clang::Type::Typedef: 6209 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type) 6210 ->getDecl() 6211 ->getUnderlyingType() 6212 .getAsOpaquePtr()); 6213 break; 6214 6215 case clang::Type::Auto: 6216 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type) 6217 ->getDeducedType() 6218 .getAsOpaquePtr()); 6219 break; 6220 6221 case clang::Type::Elaborated: 6222 count = 6223 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type) 6224 ->getNamedType() 6225 .getAsOpaquePtr()); 6226 break; 6227 6228 case clang::Type::Paren: 6229 count = GetNumVirtualBaseClasses( 6230 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); 6231 break; 6232 6233 default: 6234 break; 6235 } 6236 return count; 6237} 6238 6239CompilerType ClangASTContext::GetDirectBaseClassAtIndex( 6240 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) { 6241 clang::QualType qual_type(GetCanonicalQualType(type)); 6242 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 6243 switch (type_class) { 6244 case clang::Type::Record: 6245 if (GetCompleteType(type)) { 6246 const clang::CXXRecordDecl *cxx_record_decl = 6247 qual_type->getAsCXXRecordDecl(); 6248 if (cxx_record_decl) { 6249 uint32_t curr_idx = 0; 6250 clang::CXXRecordDecl::base_class_const_iterator base_class, 6251 base_class_end; 6252 for (base_class = cxx_record_decl->bases_begin(), 6253 base_class_end = cxx_record_decl->bases_end(); 6254 base_class != base_class_end; ++base_class, ++curr_idx) { 6255 if (curr_idx == idx) { 6256 if (bit_offset_ptr) { 6257 const clang::ASTRecordLayout &record_layout = 6258 getASTContext()->getASTRecordLayout(cxx_record_decl); 6259 const clang::CXXRecordDecl *base_class_decl = 6260 llvm::cast<clang::CXXRecordDecl>( 6261 base_class->getType() 6262 ->getAs<clang::RecordType>() 6263 ->getDecl()); 6264 if (base_class->isVirtual()) 6265 *bit_offset_ptr = 6266 record_layout.getVBaseClassOffset(base_class_decl) 6267 .getQuantity() * 6268 8; 6269 else 6270 *bit_offset_ptr = 6271 record_layout.getBaseClassOffset(base_class_decl) 6272 .getQuantity() * 6273 8; 6274 } 6275 return CompilerType(this, base_class->getType().getAsOpaquePtr()); 6276 } 6277 } 6278 } 6279 } 6280 break; 6281 6282 case clang::Type::ObjCObjectPointer: 6283 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr); 6284 6285 case clang::Type::ObjCObject: 6286 if (idx == 0 && GetCompleteType(type)) { 6287 const clang::ObjCObjectType *objc_class_type = 6288 qual_type->getAsObjCQualifiedInterfaceType(); 6289 if (objc_class_type) { 6290 clang::ObjCInterfaceDecl *class_interface_decl = 6291 objc_class_type->getInterface(); 6292 6293 if (class_interface_decl) { 6294 clang::ObjCInterfaceDecl *superclass_interface_decl = 6295 class_interface_decl->getSuperClass(); 6296 if (superclass_interface_decl) { 6297 if (bit_offset_ptr) 6298 *bit_offset_ptr = 0; 6299 return CompilerType(getASTContext(), 6300 getASTContext()->getObjCInterfaceType( 6301 superclass_interface_decl)); 6302 } 6303 } 6304 } 6305 } 6306 break; 6307 case clang::Type::ObjCInterface: 6308 if (idx == 0 && GetCompleteType(type)) { 6309 const clang::ObjCObjectType *objc_interface_type = 6310 qual_type->getAs<clang::ObjCInterfaceType>(); 6311 if (objc_interface_type) { 6312 clang::ObjCInterfaceDecl *class_interface_decl = 6313 objc_interface_type->getInterface(); 6314 6315 if (class_interface_decl) { 6316 clang::ObjCInterfaceDecl *superclass_interface_decl = 6317 class_interface_decl->getSuperClass(); 6318 if (superclass_interface_decl) { 6319 if (bit_offset_ptr) 6320 *bit_offset_ptr = 0; 6321 return CompilerType(getASTContext(), 6322 getASTContext()->getObjCInterfaceType( 6323 superclass_interface_decl)); 6324 } 6325 } 6326 } 6327 } 6328 break; 6329 6330 case clang::Type::Typedef: 6331 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type) 6332 ->getDecl() 6333 ->getUnderlyingType() 6334 .getAsOpaquePtr(), 6335 idx, bit_offset_ptr); 6336 6337 case clang::Type::Auto: 6338 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type) 6339 ->getDeducedType() 6340 .getAsOpaquePtr(), 6341 idx, bit_offset_ptr); 6342 6343 case clang::Type::Elaborated: 6344 return GetDirectBaseClassAtIndex( 6345 llvm::cast<clang::ElaboratedType>(qual_type) 6346 ->getNamedType() 6347 .getAsOpaquePtr(), 6348 idx, bit_offset_ptr); 6349 6350 case clang::Type::Paren: 6351 return GetDirectBaseClassAtIndex( 6352 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), 6353 idx, bit_offset_ptr); 6354 6355 default: 6356 break; 6357 } 6358 return CompilerType(); 6359} 6360 6361CompilerType ClangASTContext::GetVirtualBaseClassAtIndex( 6362 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) { 6363 clang::QualType qual_type(GetCanonicalQualType(type)); 6364 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 6365 switch (type_class) { 6366 case clang::Type::Record: 6367 if (GetCompleteType(type)) { 6368 const clang::CXXRecordDecl *cxx_record_decl = 6369 qual_type->getAsCXXRecordDecl(); 6370 if (cxx_record_decl) { 6371 uint32_t curr_idx = 0; 6372 clang::CXXRecordDecl::base_class_const_iterator base_class, 6373 base_class_end; 6374 for (base_class = cxx_record_decl->vbases_begin(), 6375 base_class_end = cxx_record_decl->vbases_end(); 6376 base_class != base_class_end; ++base_class, ++curr_idx) { 6377 if (curr_idx == idx) { 6378 if (bit_offset_ptr) { 6379 const clang::ASTRecordLayout &record_layout = 6380 getASTContext()->getASTRecordLayout(cxx_record_decl); 6381 const clang::CXXRecordDecl *base_class_decl = 6382 llvm::cast<clang::CXXRecordDecl>( 6383 base_class->getType() 6384 ->getAs<clang::RecordType>() 6385 ->getDecl()); 6386 *bit_offset_ptr = 6387 record_layout.getVBaseClassOffset(base_class_decl) 6388 .getQuantity() * 6389 8; 6390 } 6391 return CompilerType(this, base_class->getType().getAsOpaquePtr()); 6392 } 6393 } 6394 } 6395 } 6396 break; 6397 6398 case clang::Type::Typedef: 6399 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type) 6400 ->getDecl() 6401 ->getUnderlyingType() 6402 .getAsOpaquePtr(), 6403 idx, bit_offset_ptr); 6404 6405 case clang::Type::Auto: 6406 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type) 6407 ->getDeducedType() 6408 .getAsOpaquePtr(), 6409 idx, bit_offset_ptr); 6410 6411 case clang::Type::Elaborated: 6412 return GetVirtualBaseClassAtIndex( 6413 llvm::cast<clang::ElaboratedType>(qual_type) 6414 ->getNamedType() 6415 .getAsOpaquePtr(), 6416 idx, bit_offset_ptr); 6417 6418 case clang::Type::Paren: 6419 return GetVirtualBaseClassAtIndex( 6420 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), 6421 idx, bit_offset_ptr); 6422 6423 default: 6424 break; 6425 } 6426 return CompilerType(); 6427} 6428 6429// If a pointer to a pointee type (the clang_type arg) says that it has no 6430// children, then we either need to trust it, or override it and return a 6431// different result. For example, an "int *" has one child that is an integer, 6432// but a function pointer doesn't have any children. Likewise if a Record type 6433// claims it has no children, then there really is nothing to show. 6434uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) { 6435 if (type.isNull()) 6436 return 0; 6437 6438 clang::QualType qual_type(type.getCanonicalType()); 6439 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 6440 switch (type_class) { 6441 case clang::Type::Builtin: 6442 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { 6443 case clang::BuiltinType::UnknownAny: 6444 case clang::BuiltinType::Void: 6445 case clang::BuiltinType::NullPtr: 6446 case clang::BuiltinType::OCLEvent: 6447 case clang::BuiltinType::OCLImage1dRO: 6448 case clang::BuiltinType::OCLImage1dWO: 6449 case clang::BuiltinType::OCLImage1dRW: 6450 case clang::BuiltinType::OCLImage1dArrayRO: 6451 case clang::BuiltinType::OCLImage1dArrayWO: 6452 case clang::BuiltinType::OCLImage1dArrayRW: 6453 case clang::BuiltinType::OCLImage1dBufferRO: 6454 case clang::BuiltinType::OCLImage1dBufferWO: 6455 case clang::BuiltinType::OCLImage1dBufferRW: 6456 case clang::BuiltinType::OCLImage2dRO: 6457 case clang::BuiltinType::OCLImage2dWO: 6458 case clang::BuiltinType::OCLImage2dRW: 6459 case clang::BuiltinType::OCLImage2dArrayRO: 6460 case clang::BuiltinType::OCLImage2dArrayWO: 6461 case clang::BuiltinType::OCLImage2dArrayRW: 6462 case clang::BuiltinType::OCLImage3dRO: 6463 case clang::BuiltinType::OCLImage3dWO: 6464 case clang::BuiltinType::OCLImage3dRW: 6465 case clang::BuiltinType::OCLSampler: 6466 return 0; 6467 case clang::BuiltinType::Bool: 6468 case clang::BuiltinType::Char_U: 6469 case clang::BuiltinType::UChar: 6470 case clang::BuiltinType::WChar_U: 6471 case clang::BuiltinType::Char16: 6472 case clang::BuiltinType::Char32: 6473 case clang::BuiltinType::UShort: 6474 case clang::BuiltinType::UInt: 6475 case clang::BuiltinType::ULong: 6476 case clang::BuiltinType::ULongLong: 6477 case clang::BuiltinType::UInt128: 6478 case clang::BuiltinType::Char_S: 6479 case clang::BuiltinType::SChar: 6480 case clang::BuiltinType::WChar_S: 6481 case clang::BuiltinType::Short: 6482 case clang::BuiltinType::Int: 6483 case clang::BuiltinType::Long: 6484 case clang::BuiltinType::LongLong: 6485 case clang::BuiltinType::Int128: 6486 case clang::BuiltinType::Float: 6487 case clang::BuiltinType::Double: 6488 case clang::BuiltinType::LongDouble: 6489 case clang::BuiltinType::Dependent: 6490 case clang::BuiltinType::Overload: 6491 case clang::BuiltinType::ObjCId: 6492 case clang::BuiltinType::ObjCClass: 6493 case clang::BuiltinType::ObjCSel: 6494 case clang::BuiltinType::BoundMember: 6495 case clang::BuiltinType::Half: 6496 case clang::BuiltinType::ARCUnbridgedCast: 6497 case clang::BuiltinType::PseudoObject: 6498 case clang::BuiltinType::BuiltinFn: 6499 case clang::BuiltinType::OMPArraySection: 6500 return 1; 6501 default: 6502 return 0; 6503 } 6504 break; 6505 6506 case clang::Type::Complex: 6507 return 1; 6508 case clang::Type::Pointer: 6509 return 1; 6510 case clang::Type::BlockPointer: 6511 return 0; // If block pointers don't have debug info, then no children for 6512 // them 6513 case clang::Type::LValueReference: 6514 return 1; 6515 case clang::Type::RValueReference: 6516 return 1; 6517 case clang::Type::MemberPointer: 6518 return 0; 6519 case clang::Type::ConstantArray: 6520 return 0; 6521 case clang::Type::IncompleteArray: 6522 return 0; 6523 case clang::Type::VariableArray: 6524 return 0; 6525 case clang::Type::DependentSizedArray: 6526 return 0; 6527 case clang::Type::DependentSizedExtVector: 6528 return 0; 6529 case clang::Type::Vector: 6530 return 0; 6531 case clang::Type::ExtVector: 6532 return 0; 6533 case clang::Type::FunctionProto: 6534 return 0; // When we function pointers, they have no children... 6535 case clang::Type::FunctionNoProto: 6536 return 0; // When we function pointers, they have no children... 6537 case clang::Type::UnresolvedUsing: 6538 return 0; 6539 case clang::Type::Paren: 6540 return GetNumPointeeChildren( 6541 llvm::cast<clang::ParenType>(qual_type)->desugar()); 6542 case clang::Type::Typedef: 6543 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type) 6544 ->getDecl() 6545 ->getUnderlyingType()); 6546 case clang::Type::Auto: 6547 return GetNumPointeeChildren( 6548 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()); 6549 case clang::Type::Elaborated: 6550 return GetNumPointeeChildren( 6551 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()); 6552 case clang::Type::TypeOfExpr: 6553 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type) 6554 ->getUnderlyingExpr() 6555 ->getType()); 6556 case clang::Type::TypeOf: 6557 return GetNumPointeeChildren( 6558 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()); 6559 case clang::Type::Decltype: 6560 return GetNumPointeeChildren( 6561 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()); 6562 case clang::Type::Record: 6563 return 0; 6564 case clang::Type::Enum: 6565 return 1; 6566 case clang::Type::TemplateTypeParm: 6567 return 1; 6568 case clang::Type::SubstTemplateTypeParm: 6569 return 1; 6570 case clang::Type::TemplateSpecialization: 6571 return 1; 6572 case clang::Type::InjectedClassName: 6573 return 0; 6574 case clang::Type::DependentName: 6575 return 1; 6576 case clang::Type::DependentTemplateSpecialization: 6577 return 1; 6578 case clang::Type::ObjCObject: 6579 return 0; 6580 case clang::Type::ObjCInterface: 6581 return 0; 6582 case clang::Type::ObjCObjectPointer: 6583 return 1; 6584 default: 6585 break; 6586 } 6587 return 0; 6588} 6589 6590CompilerType ClangASTContext::GetChildCompilerTypeAtIndex( 6591 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, 6592 bool transparent_pointers, bool omit_empty_base_classes, 6593 bool ignore_array_bounds, std::string &child_name, 6594 uint32_t &child_byte_size, int32_t &child_byte_offset, 6595 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, 6596 bool &child_is_base_class, bool &child_is_deref_of_parent, 6597 ValueObject *valobj, uint64_t &language_flags) { 6598 if (!type) 6599 return CompilerType(); 6600 6601 auto get_exe_scope = [&exe_ctx]() { 6602 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr; 6603 }; 6604 6605 clang::QualType parent_qual_type(GetCanonicalQualType(type)); 6606 const clang::Type::TypeClass parent_type_class = 6607 parent_qual_type->getTypeClass(); 6608 child_bitfield_bit_size = 0; 6609 child_bitfield_bit_offset = 0; 6610 child_is_base_class = false; 6611 language_flags = 0; 6612 6613 const bool idx_is_valid = 6614 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx); 6615 int32_t bit_offset; 6616 switch (parent_type_class) { 6617 case clang::Type::Builtin: 6618 if (idx_is_valid) { 6619 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) { 6620 case clang::BuiltinType::ObjCId: 6621 case clang::BuiltinType::ObjCClass: 6622 child_name = "isa"; 6623 child_byte_size = 6624 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / 6625 CHAR_BIT; 6626 return CompilerType(getASTContext(), 6627 getASTContext()->ObjCBuiltinClassTy); 6628 6629 default: 6630 break; 6631 } 6632 } 6633 break; 6634 6635 case clang::Type::Record: 6636 if (idx_is_valid && GetCompleteType(type)) { 6637 const clang::RecordType *record_type = 6638 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr()); 6639 const clang::RecordDecl *record_decl = record_type->getDecl(); 6640 assert(record_decl); 6641 const clang::ASTRecordLayout &record_layout = 6642 getASTContext()->getASTRecordLayout(record_decl); 6643 uint32_t child_idx = 0; 6644 6645 const clang::CXXRecordDecl *cxx_record_decl = 6646 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); 6647 if (cxx_record_decl) { 6648 // We might have base classes to print out first 6649 clang::CXXRecordDecl::base_class_const_iterator base_class, 6650 base_class_end; 6651 for (base_class = cxx_record_decl->bases_begin(), 6652 base_class_end = cxx_record_decl->bases_end(); 6653 base_class != base_class_end; ++base_class) { 6654 const clang::CXXRecordDecl *base_class_decl = nullptr; 6655 6656 // Skip empty base classes 6657 if (omit_empty_base_classes) { 6658 base_class_decl = llvm::cast<clang::CXXRecordDecl>( 6659 base_class->getType()->getAs<clang::RecordType>()->getDecl()); 6660 if (!ClangASTContext::RecordHasFields(base_class_decl)) 6661 continue; 6662 } 6663 6664 if (idx == child_idx) { 6665 if (base_class_decl == nullptr) 6666 base_class_decl = llvm::cast<clang::CXXRecordDecl>( 6667 base_class->getType()->getAs<clang::RecordType>()->getDecl()); 6668 6669 if (base_class->isVirtual()) { 6670 bool handled = false; 6671 if (valobj) { 6672 clang::VTableContextBase *vtable_ctx = 6673 getASTContext()->getVTableContext(); 6674 if (vtable_ctx) 6675 handled = GetVBaseBitOffset(*vtable_ctx, *valobj, 6676 record_layout, cxx_record_decl, 6677 base_class_decl, bit_offset); 6678 } 6679 if (!handled) 6680 bit_offset = record_layout.getVBaseClassOffset(base_class_decl) 6681 .getQuantity() * 6682 8; 6683 } else 6684 bit_offset = record_layout.getBaseClassOffset(base_class_decl) 6685 .getQuantity() * 6686 8; 6687 6688 // Base classes should be a multiple of 8 bits in size 6689 child_byte_offset = bit_offset / 8; 6690 CompilerType base_class_clang_type(getASTContext(), 6691 base_class->getType()); 6692 child_name = base_class_clang_type.GetTypeName().AsCString(""); 6693 Optional<uint64_t> size = 6694 base_class_clang_type.GetBitSize(get_exe_scope()); 6695 if (!size) 6696 return {}; 6697 uint64_t base_class_clang_type_bit_size = *size; 6698 6699 // Base classes bit sizes should be a multiple of 8 bits in size 6700 assert(base_class_clang_type_bit_size % 8 == 0); 6701 child_byte_size = base_class_clang_type_bit_size / 8; 6702 child_is_base_class = true; 6703 return base_class_clang_type; 6704 } 6705 // We don't increment the child index in the for loop since we might 6706 // be skipping empty base classes 6707 ++child_idx; 6708 } 6709 } 6710 // Make sure index is in range... 6711 uint32_t field_idx = 0; 6712 clang::RecordDecl::field_iterator field, field_end; 6713 for (field = record_decl->field_begin(), 6714 field_end = record_decl->field_end(); 6715 field != field_end; ++field, ++field_idx, ++child_idx) { 6716 if (idx == child_idx) { 6717 // Print the member type if requested 6718 // Print the member name and equal sign 6719 child_name.assign(field->getNameAsString()); 6720 6721 // Figure out the type byte size (field_type_info.first) and 6722 // alignment (field_type_info.second) from the AST context. 6723 CompilerType field_clang_type(getASTContext(), field->getType()); 6724 assert(field_idx < record_layout.getFieldCount()); 6725 Optional<uint64_t> size = 6726 field_clang_type.GetByteSize(get_exe_scope()); 6727 if (!size) 6728 return {}; 6729 child_byte_size = *size; 6730 const uint32_t child_bit_size = child_byte_size * 8; 6731 6732 // Figure out the field offset within the current struct/union/class 6733 // type 6734 bit_offset = record_layout.getFieldOffset(field_idx); 6735 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field, 6736 child_bitfield_bit_size)) { 6737 child_bitfield_bit_offset = bit_offset % child_bit_size; 6738 const uint32_t child_bit_offset = 6739 bit_offset - child_bitfield_bit_offset; 6740 child_byte_offset = child_bit_offset / 8; 6741 } else { 6742 child_byte_offset = bit_offset / 8; 6743 } 6744 6745 return field_clang_type; 6746 } 6747 } 6748 } 6749 break; 6750 6751 case clang::Type::ObjCObject: 6752 case clang::Type::ObjCInterface: 6753 if (idx_is_valid && GetCompleteType(type)) { 6754 const clang::ObjCObjectType *objc_class_type = 6755 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr()); 6756 assert(objc_class_type); 6757 if (objc_class_type) { 6758 uint32_t child_idx = 0; 6759 clang::ObjCInterfaceDecl *class_interface_decl = 6760 objc_class_type->getInterface(); 6761 6762 if (class_interface_decl) { 6763 6764 const clang::ASTRecordLayout &interface_layout = 6765 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl); 6766 clang::ObjCInterfaceDecl *superclass_interface_decl = 6767 class_interface_decl->getSuperClass(); 6768 if (superclass_interface_decl) { 6769 if (omit_empty_base_classes) { 6770 CompilerType base_class_clang_type( 6771 getASTContext(), getASTContext()->getObjCInterfaceType( 6772 superclass_interface_decl)); 6773 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes, 6774 exe_ctx) > 0) { 6775 if (idx == 0) { 6776 clang::QualType ivar_qual_type( 6777 getASTContext()->getObjCInterfaceType( 6778 superclass_interface_decl)); 6779 6780 child_name.assign( 6781 superclass_interface_decl->getNameAsString()); 6782 6783 clang::TypeInfo ivar_type_info = 6784 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr()); 6785 6786 child_byte_size = ivar_type_info.Width / 8; 6787 child_byte_offset = 0; 6788 child_is_base_class = true; 6789 6790 return CompilerType(getASTContext(), ivar_qual_type); 6791 } 6792 6793 ++child_idx; 6794 } 6795 } else 6796 ++child_idx; 6797 } 6798 6799 const uint32_t superclass_idx = child_idx; 6800 6801 if (idx < (child_idx + class_interface_decl->ivar_size())) { 6802 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, 6803 ivar_end = class_interface_decl->ivar_end(); 6804 6805 for (ivar_pos = class_interface_decl->ivar_begin(); 6806 ivar_pos != ivar_end; ++ivar_pos) { 6807 if (child_idx == idx) { 6808 clang::ObjCIvarDecl *ivar_decl = *ivar_pos; 6809 6810 clang::QualType ivar_qual_type(ivar_decl->getType()); 6811 6812 child_name.assign(ivar_decl->getNameAsString()); 6813 6814 clang::TypeInfo ivar_type_info = 6815 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr()); 6816 6817 child_byte_size = ivar_type_info.Width / 8; 6818 6819 // Figure out the field offset within the current 6820 // struct/union/class type For ObjC objects, we can't trust the 6821 // bit offset we get from the Clang AST, since that doesn't 6822 // account for the space taken up by unbacked properties, or 6823 // from the changing size of base classes that are newer than 6824 // this class. So if we have a process around that we can ask 6825 // about this object, do so. 6826 child_byte_offset = LLDB_INVALID_IVAR_OFFSET; 6827 Process *process = nullptr; 6828 if (exe_ctx) 6829 process = exe_ctx->GetProcessPtr(); 6830 if (process) { 6831 ObjCLanguageRuntime *objc_runtime = 6832 process->GetObjCLanguageRuntime(); 6833 if (objc_runtime != nullptr) { 6834 CompilerType parent_ast_type(getASTContext(), 6835 parent_qual_type); 6836 child_byte_offset = objc_runtime->GetByteOffsetForIvar( 6837 parent_ast_type, ivar_decl->getNameAsString().c_str()); 6838 } 6839 } 6840 6841 // Setting this to INT32_MAX to make sure we don't compute it 6842 // twice... 6843 bit_offset = INT32_MAX; 6844 6845 if (child_byte_offset == 6846 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) { 6847 bit_offset = interface_layout.getFieldOffset(child_idx - 6848 superclass_idx); 6849 child_byte_offset = bit_offset / 8; 6850 } 6851 6852 // Note, the ObjC Ivar Byte offset is just that, it doesn't 6853 // account for the bit offset of a bitfield within its 6854 // containing object. So regardless of where we get the byte 6855 // offset from, we still need to get the bit offset for 6856 // bitfields from the layout. 6857 6858 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl, 6859 child_bitfield_bit_size)) { 6860 if (bit_offset == INT32_MAX) 6861 bit_offset = interface_layout.getFieldOffset( 6862 child_idx - superclass_idx); 6863 6864 child_bitfield_bit_offset = bit_offset % 8; 6865 } 6866 return CompilerType(getASTContext(), ivar_qual_type); 6867 } 6868 ++child_idx; 6869 } 6870 } 6871 } 6872 } 6873 } 6874 break; 6875 6876 case clang::Type::ObjCObjectPointer: 6877 if (idx_is_valid) { 6878 CompilerType pointee_clang_type(GetPointeeType(type)); 6879 6880 if (transparent_pointers && pointee_clang_type.IsAggregateType()) { 6881 child_is_deref_of_parent = false; 6882 bool tmp_child_is_deref_of_parent = false; 6883 return pointee_clang_type.GetChildCompilerTypeAtIndex( 6884 exe_ctx, idx, transparent_pointers, omit_empty_base_classes, 6885 ignore_array_bounds, child_name, child_byte_size, child_byte_offset, 6886 child_bitfield_bit_size, child_bitfield_bit_offset, 6887 child_is_base_class, tmp_child_is_deref_of_parent, valobj, 6888 language_flags); 6889 } else { 6890 child_is_deref_of_parent = true; 6891 const char *parent_name = 6892 valobj ? valobj->GetName().GetCString() : NULL; 6893 if (parent_name) { 6894 child_name.assign(1, '*'); 6895 child_name += parent_name; 6896 } 6897 6898 // We have a pointer to an simple type 6899 if (idx == 0 && pointee_clang_type.GetCompleteType()) { 6900 if (Optional<uint64_t> size = 6901 pointee_clang_type.GetByteSize(get_exe_scope())) { 6902 child_byte_size = *size; 6903 child_byte_offset = 0; 6904 return pointee_clang_type; 6905 } 6906 } 6907 } 6908 } 6909 break; 6910 6911 case clang::Type::Vector: 6912 case clang::Type::ExtVector: 6913 if (idx_is_valid) { 6914 const clang::VectorType *array = 6915 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr()); 6916 if (array) { 6917 CompilerType element_type(getASTContext(), array->getElementType()); 6918 if (element_type.GetCompleteType()) { 6919 char element_name[64]; 6920 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]", 6921 static_cast<uint64_t>(idx)); 6922 child_name.assign(element_name); 6923 if (Optional<uint64_t> size = 6924 element_type.GetByteSize(get_exe_scope())) { 6925 child_byte_size = *size; 6926 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; 6927 return element_type; 6928 } 6929 } 6930 } 6931 } 6932 break; 6933 6934 case clang::Type::ConstantArray: 6935 case clang::Type::IncompleteArray: 6936 if (ignore_array_bounds || idx_is_valid) { 6937 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe(); 6938 if (array) { 6939 CompilerType element_type(getASTContext(), array->getElementType()); 6940 if (element_type.GetCompleteType()) { 6941 child_name = llvm::formatv("[{0}]", idx); 6942 if (Optional<uint64_t> size = 6943 element_type.GetByteSize(get_exe_scope())) { 6944 child_byte_size = *size; 6945 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; 6946 return element_type; 6947 } 6948 } 6949 } 6950 } 6951 break; 6952 6953 case clang::Type::Pointer: { 6954 CompilerType pointee_clang_type(GetPointeeType(type)); 6955 6956 // Don't dereference "void *" pointers 6957 if (pointee_clang_type.IsVoidType()) 6958 return CompilerType(); 6959 6960 if (transparent_pointers && pointee_clang_type.IsAggregateType()) { 6961 child_is_deref_of_parent = false; 6962 bool tmp_child_is_deref_of_parent = false; 6963 return pointee_clang_type.GetChildCompilerTypeAtIndex( 6964 exe_ctx, idx, transparent_pointers, omit_empty_base_classes, 6965 ignore_array_bounds, child_name, child_byte_size, child_byte_offset, 6966 child_bitfield_bit_size, child_bitfield_bit_offset, 6967 child_is_base_class, tmp_child_is_deref_of_parent, valobj, 6968 language_flags); 6969 } else { 6970 child_is_deref_of_parent = true; 6971 6972 const char *parent_name = 6973 valobj ? valobj->GetName().GetCString() : NULL; 6974 if (parent_name) { 6975 child_name.assign(1, '*'); 6976 child_name += parent_name; 6977 } 6978 6979 // We have a pointer to an simple type 6980 if (idx == 0) { 6981 if (Optional<uint64_t> size = 6982 pointee_clang_type.GetByteSize(get_exe_scope())) { 6983 child_byte_size = *size; 6984 child_byte_offset = 0; 6985 return pointee_clang_type; 6986 } 6987 } 6988 } 6989 break; 6990 } 6991 6992 case clang::Type::LValueReference: 6993 case clang::Type::RValueReference: 6994 if (idx_is_valid) { 6995 const clang::ReferenceType *reference_type = 6996 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr()); 6997 CompilerType pointee_clang_type(getASTContext(), 6998 reference_type->getPointeeType()); 6999 if (transparent_pointers && pointee_clang_type.IsAggregateType()) { 7000 child_is_deref_of_parent = false; 7001 bool tmp_child_is_deref_of_parent = false; 7002 return pointee_clang_type.GetChildCompilerTypeAtIndex( 7003 exe_ctx, idx, transparent_pointers, omit_empty_base_classes, 7004 ignore_array_bounds, child_name, child_byte_size, child_byte_offset, 7005 child_bitfield_bit_size, child_bitfield_bit_offset, 7006 child_is_base_class, tmp_child_is_deref_of_parent, valobj, 7007 language_flags); 7008 } else { 7009 const char *parent_name = 7010 valobj ? valobj->GetName().GetCString() : NULL; 7011 if (parent_name) { 7012 child_name.assign(1, '&'); 7013 child_name += parent_name; 7014 } 7015 7016 // We have a pointer to an simple type 7017 if (idx == 0) { 7018 if (Optional<uint64_t> size = 7019 pointee_clang_type.GetByteSize(get_exe_scope())) { 7020 child_byte_size = *size; 7021 child_byte_offset = 0; 7022 return pointee_clang_type; 7023 } 7024 } 7025 } 7026 } 7027 break; 7028 7029 case clang::Type::Typedef: { 7030 CompilerType typedefed_clang_type( 7031 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type) 7032 ->getDecl() 7033 ->getUnderlyingType()); 7034 return typedefed_clang_type.GetChildCompilerTypeAtIndex( 7035 exe_ctx, idx, transparent_pointers, omit_empty_base_classes, 7036 ignore_array_bounds, child_name, child_byte_size, child_byte_offset, 7037 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, 7038 child_is_deref_of_parent, valobj, language_flags); 7039 } break; 7040 7041 case clang::Type::Auto: { 7042 CompilerType elaborated_clang_type( 7043 getASTContext(), 7044 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType()); 7045 return elaborated_clang_type.GetChildCompilerTypeAtIndex( 7046 exe_ctx, idx, transparent_pointers, omit_empty_base_classes, 7047 ignore_array_bounds, child_name, child_byte_size, child_byte_offset, 7048 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, 7049 child_is_deref_of_parent, valobj, language_flags); 7050 } 7051 7052 case clang::Type::Elaborated: { 7053 CompilerType elaborated_clang_type( 7054 getASTContext(), 7055 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType()); 7056 return elaborated_clang_type.GetChildCompilerTypeAtIndex( 7057 exe_ctx, idx, transparent_pointers, omit_empty_base_classes, 7058 ignore_array_bounds, child_name, child_byte_size, child_byte_offset, 7059 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, 7060 child_is_deref_of_parent, valobj, language_flags); 7061 } 7062 7063 case clang::Type::Paren: { 7064 CompilerType paren_clang_type( 7065 getASTContext(), 7066 llvm::cast<clang::ParenType>(parent_qual_type)->desugar()); 7067 return paren_clang_type.GetChildCompilerTypeAtIndex( 7068 exe_ctx, idx, transparent_pointers, omit_empty_base_classes, 7069 ignore_array_bounds, child_name, child_byte_size, child_byte_offset, 7070 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, 7071 child_is_deref_of_parent, valobj, language_flags); 7072 } 7073 7074 default: 7075 break; 7076 } 7077 return CompilerType(); 7078} 7079 7080static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl, 7081 const clang::CXXBaseSpecifier *base_spec, 7082 bool omit_empty_base_classes) { 7083 uint32_t child_idx = 0; 7084 7085 const clang::CXXRecordDecl *cxx_record_decl = 7086 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); 7087 7088 // const char *super_name = record_decl->getNameAsCString(); 7089 // const char *base_name = 7090 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString(); 7091 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name); 7092 // 7093 if (cxx_record_decl) { 7094 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 7095 for (base_class = cxx_record_decl->bases_begin(), 7096 base_class_end = cxx_record_decl->bases_end(); 7097 base_class != base_class_end; ++base_class) { 7098 if (omit_empty_base_classes) { 7099 if (BaseSpecifierIsEmpty(base_class)) 7100 continue; 7101 } 7102 7103 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", 7104 // super_name, base_name, 7105 // child_idx, 7106 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString()); 7107 // 7108 // 7109 if (base_class == base_spec) 7110 return child_idx; 7111 ++child_idx; 7112 } 7113 } 7114 7115 return UINT32_MAX; 7116} 7117 7118static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl, 7119 clang::NamedDecl *canonical_decl, 7120 bool omit_empty_base_classes) { 7121 uint32_t child_idx = ClangASTContext::GetNumBaseClasses( 7122 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl), 7123 omit_empty_base_classes); 7124 7125 clang::RecordDecl::field_iterator field, field_end; 7126 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 7127 field != field_end; ++field, ++child_idx) { 7128 if (field->getCanonicalDecl() == canonical_decl) 7129 return child_idx; 7130 } 7131 7132 return UINT32_MAX; 7133} 7134 7135// Look for a child member (doesn't include base classes, but it does include 7136// their members) in the type hierarchy. Returns an index path into 7137// "clang_type" on how to reach the appropriate member. 7138// 7139// class A 7140// { 7141// public: 7142// int m_a; 7143// int m_b; 7144// }; 7145// 7146// class B 7147// { 7148// }; 7149// 7150// class C : 7151// public B, 7152// public A 7153// { 7154// }; 7155// 7156// If we have a clang type that describes "class C", and we wanted to looked 7157// "m_b" in it: 7158// 7159// With omit_empty_base_classes == false we would get an integer array back 7160// with: { 1, 1 } The first index 1 is the child index for "class A" within 7161// class C The second index 1 is the child index for "m_b" within class A 7162// 7163// With omit_empty_base_classes == true we would get an integer array back 7164// with: { 0, 1 } The first index 0 is the child index for "class A" within 7165// class C (since class B doesn't have any members it doesn't count) The second 7166// index 1 is the child index for "m_b" within class A 7167 7168size_t ClangASTContext::GetIndexOfChildMemberWithName( 7169 lldb::opaque_compiler_type_t type, const char *name, 7170 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) { 7171 if (type && name && name[0]) { 7172 clang::QualType qual_type(GetCanonicalQualType(type)); 7173 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 7174 switch (type_class) { 7175 case clang::Type::Record: 7176 if (GetCompleteType(type)) { 7177 const clang::RecordType *record_type = 7178 llvm::cast<clang::RecordType>(qual_type.getTypePtr()); 7179 const clang::RecordDecl *record_decl = record_type->getDecl(); 7180 7181 assert(record_decl); 7182 uint32_t child_idx = 0; 7183 7184 const clang::CXXRecordDecl *cxx_record_decl = 7185 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); 7186 7187 // Try and find a field that matches NAME 7188 clang::RecordDecl::field_iterator field, field_end; 7189 llvm::StringRef name_sref(name); 7190 for (field = record_decl->field_begin(), 7191 field_end = record_decl->field_end(); 7192 field != field_end; ++field, ++child_idx) { 7193 llvm::StringRef field_name = field->getName(); 7194 if (field_name.empty()) { 7195 CompilerType field_type(getASTContext(), field->getType()); 7196 child_indexes.push_back(child_idx); 7197 if (field_type.GetIndexOfChildMemberWithName( 7198 name, omit_empty_base_classes, child_indexes)) 7199 return child_indexes.size(); 7200 child_indexes.pop_back(); 7201 7202 } else if (field_name.equals(name_sref)) { 7203 // We have to add on the number of base classes to this index! 7204 child_indexes.push_back( 7205 child_idx + ClangASTContext::GetNumBaseClasses( 7206 cxx_record_decl, omit_empty_base_classes)); 7207 return child_indexes.size(); 7208 } 7209 } 7210 7211 if (cxx_record_decl) { 7212 const clang::RecordDecl *parent_record_decl = cxx_record_decl; 7213 7214 // printf ("parent = %s\n", parent_record_decl->getNameAsCString()); 7215 7216 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl(); 7217 // Didn't find things easily, lets let clang do its thang... 7218 clang::IdentifierInfo &ident_ref = 7219 getASTContext()->Idents.get(name_sref); 7220 clang::DeclarationName decl_name(&ident_ref); 7221 7222 clang::CXXBasePaths paths; 7223 if (cxx_record_decl->lookupInBases( 7224 [decl_name](const clang::CXXBaseSpecifier *specifier, 7225 clang::CXXBasePath &path) { 7226 return clang::CXXRecordDecl::FindOrdinaryMember( 7227 specifier, path, decl_name); 7228 }, 7229 paths)) { 7230 clang::CXXBasePaths::const_paths_iterator path, 7231 path_end = paths.end(); 7232 for (path = paths.begin(); path != path_end; ++path) { 7233 const size_t num_path_elements = path->size(); 7234 for (size_t e = 0; e < num_path_elements; ++e) { 7235 clang::CXXBasePathElement elem = (*path)[e]; 7236 7237 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base, 7238 omit_empty_base_classes); 7239 if (child_idx == UINT32_MAX) { 7240 child_indexes.clear(); 7241 return 0; 7242 } else { 7243 child_indexes.push_back(child_idx); 7244 parent_record_decl = llvm::cast<clang::RecordDecl>( 7245 elem.Base->getType() 7246 ->getAs<clang::RecordType>() 7247 ->getDecl()); 7248 } 7249 } 7250 for (clang::NamedDecl *path_decl : path->Decls) { 7251 child_idx = GetIndexForRecordChild( 7252 parent_record_decl, path_decl, omit_empty_base_classes); 7253 if (child_idx == UINT32_MAX) { 7254 child_indexes.clear(); 7255 return 0; 7256 } else { 7257 child_indexes.push_back(child_idx); 7258 } 7259 } 7260 } 7261 return child_indexes.size(); 7262 } 7263 } 7264 } 7265 break; 7266 7267 case clang::Type::ObjCObject: 7268 case clang::Type::ObjCInterface: 7269 if (GetCompleteType(type)) { 7270 llvm::StringRef name_sref(name); 7271 const clang::ObjCObjectType *objc_class_type = 7272 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); 7273 assert(objc_class_type); 7274 if (objc_class_type) { 7275 uint32_t child_idx = 0; 7276 clang::ObjCInterfaceDecl *class_interface_decl = 7277 objc_class_type->getInterface(); 7278 7279 if (class_interface_decl) { 7280 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, 7281 ivar_end = class_interface_decl->ivar_end(); 7282 clang::ObjCInterfaceDecl *superclass_interface_decl = 7283 class_interface_decl->getSuperClass(); 7284 7285 for (ivar_pos = class_interface_decl->ivar_begin(); 7286 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) { 7287 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos; 7288 7289 if (ivar_decl->getName().equals(name_sref)) { 7290 if ((!omit_empty_base_classes && superclass_interface_decl) || 7291 (omit_empty_base_classes && 7292 ObjCDeclHasIVars(superclass_interface_decl, true))) 7293 ++child_idx; 7294 7295 child_indexes.push_back(child_idx); 7296 return child_indexes.size(); 7297 } 7298 } 7299 7300 if (superclass_interface_decl) { 7301 // The super class index is always zero for ObjC classes, so we 7302 // push it onto the child indexes in case we find an ivar in our 7303 // superclass... 7304 child_indexes.push_back(0); 7305 7306 CompilerType superclass_clang_type( 7307 getASTContext(), getASTContext()->getObjCInterfaceType( 7308 superclass_interface_decl)); 7309 if (superclass_clang_type.GetIndexOfChildMemberWithName( 7310 name, omit_empty_base_classes, child_indexes)) { 7311 // We did find an ivar in a superclass so just return the 7312 // results! 7313 return child_indexes.size(); 7314 } 7315 7316 // We didn't find an ivar matching "name" in our superclass, pop 7317 // the superclass zero index that we pushed on above. 7318 child_indexes.pop_back(); 7319 } 7320 } 7321 } 7322 } 7323 break; 7324 7325 case clang::Type::ObjCObjectPointer: { 7326 CompilerType objc_object_clang_type( 7327 getASTContext(), 7328 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()) 7329 ->getPointeeType()); 7330 return objc_object_clang_type.GetIndexOfChildMemberWithName( 7331 name, omit_empty_base_classes, child_indexes); 7332 } break; 7333 7334 case clang::Type::ConstantArray: { 7335 // const clang::ConstantArrayType *array = 7336 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr()); 7337 // const uint64_t element_count = 7338 // array->getSize().getLimitedValue(); 7339 // 7340 // if (idx < element_count) 7341 // { 7342 // std::pair<uint64_t, unsigned> field_type_info = 7343 // ast->getTypeInfo(array->getElementType()); 7344 // 7345 // char element_name[32]; 7346 // ::snprintf (element_name, sizeof (element_name), 7347 // "%s[%u]", parent_name ? parent_name : "", idx); 7348 // 7349 // child_name.assign(element_name); 7350 // assert(field_type_info.first % 8 == 0); 7351 // child_byte_size = field_type_info.first / 8; 7352 // child_byte_offset = idx * child_byte_size; 7353 // return array->getElementType().getAsOpaquePtr(); 7354 // } 7355 } break; 7356 7357 // case clang::Type::MemberPointerType: 7358 // { 7359 // MemberPointerType *mem_ptr_type = 7360 // llvm::cast<MemberPointerType>(qual_type.getTypePtr()); 7361 // clang::QualType pointee_type = 7362 // mem_ptr_type->getPointeeType(); 7363 // 7364 // if (ClangASTContext::IsAggregateType 7365 // (pointee_type.getAsOpaquePtr())) 7366 // { 7367 // return GetIndexOfChildWithName (ast, 7368 // mem_ptr_type->getPointeeType().getAsOpaquePtr(), 7369 // name); 7370 // } 7371 // } 7372 // break; 7373 // 7374 case clang::Type::LValueReference: 7375 case clang::Type::RValueReference: { 7376 const clang::ReferenceType *reference_type = 7377 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); 7378 clang::QualType pointee_type(reference_type->getPointeeType()); 7379 CompilerType pointee_clang_type(getASTContext(), pointee_type); 7380 7381 if (pointee_clang_type.IsAggregateType()) { 7382 return pointee_clang_type.GetIndexOfChildMemberWithName( 7383 name, omit_empty_base_classes, child_indexes); 7384 } 7385 } break; 7386 7387 case clang::Type::Pointer: { 7388 CompilerType pointee_clang_type(GetPointeeType(type)); 7389 7390 if (pointee_clang_type.IsAggregateType()) { 7391 return pointee_clang_type.GetIndexOfChildMemberWithName( 7392 name, omit_empty_base_classes, child_indexes); 7393 } 7394 } break; 7395 7396 case clang::Type::Typedef: 7397 return CompilerType(getASTContext(), 7398 llvm::cast<clang::TypedefType>(qual_type) 7399 ->getDecl() 7400 ->getUnderlyingType()) 7401 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, 7402 child_indexes); 7403 7404 case clang::Type::Auto: 7405 return CompilerType( 7406 getASTContext(), 7407 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) 7408 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, 7409 child_indexes); 7410 7411 case clang::Type::Elaborated: 7412 return CompilerType( 7413 getASTContext(), 7414 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) 7415 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, 7416 child_indexes); 7417 7418 case clang::Type::Paren: 7419 return CompilerType(getASTContext(), 7420 llvm::cast<clang::ParenType>(qual_type)->desugar()) 7421 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, 7422 child_indexes); 7423 7424 default: 7425 break; 7426 } 7427 } 7428 return 0; 7429} 7430 7431// Get the index of the child of "clang_type" whose name matches. This function 7432// doesn't descend into the children, but only looks one level deep and name 7433// matches can include base class names. 7434 7435uint32_t 7436ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, 7437 const char *name, 7438 bool omit_empty_base_classes) { 7439 if (type && name && name[0]) { 7440 clang::QualType qual_type(GetCanonicalQualType(type)); 7441 7442 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 7443 7444 switch (type_class) { 7445 case clang::Type::Record: 7446 if (GetCompleteType(type)) { 7447 const clang::RecordType *record_type = 7448 llvm::cast<clang::RecordType>(qual_type.getTypePtr()); 7449 const clang::RecordDecl *record_decl = record_type->getDecl(); 7450 7451 assert(record_decl); 7452 uint32_t child_idx = 0; 7453 7454 const clang::CXXRecordDecl *cxx_record_decl = 7455 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); 7456 7457 if (cxx_record_decl) { 7458 clang::CXXRecordDecl::base_class_const_iterator base_class, 7459 base_class_end; 7460 for (base_class = cxx_record_decl->bases_begin(), 7461 base_class_end = cxx_record_decl->bases_end(); 7462 base_class != base_class_end; ++base_class) { 7463 // Skip empty base classes 7464 clang::CXXRecordDecl *base_class_decl = 7465 llvm::cast<clang::CXXRecordDecl>( 7466 base_class->getType() 7467 ->getAs<clang::RecordType>() 7468 ->getDecl()); 7469 if (omit_empty_base_classes && 7470 !ClangASTContext::RecordHasFields(base_class_decl)) 7471 continue; 7472 7473 CompilerType base_class_clang_type(getASTContext(), 7474 base_class->getType()); 7475 std::string base_class_type_name( 7476 base_class_clang_type.GetTypeName().AsCString("")); 7477 if (base_class_type_name == name) 7478 return child_idx; 7479 ++child_idx; 7480 } 7481 } 7482 7483 // Try and find a field that matches NAME 7484 clang::RecordDecl::field_iterator field, field_end; 7485 llvm::StringRef name_sref(name); 7486 for (field = record_decl->field_begin(), 7487 field_end = record_decl->field_end(); 7488 field != field_end; ++field, ++child_idx) { 7489 if (field->getName().equals(name_sref)) 7490 return child_idx; 7491 } 7492 } 7493 break; 7494 7495 case clang::Type::ObjCObject: 7496 case clang::Type::ObjCInterface: 7497 if (GetCompleteType(type)) { 7498 llvm::StringRef name_sref(name); 7499 const clang::ObjCObjectType *objc_class_type = 7500 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); 7501 assert(objc_class_type); 7502 if (objc_class_type) { 7503 uint32_t child_idx = 0; 7504 clang::ObjCInterfaceDecl *class_interface_decl = 7505 objc_class_type->getInterface(); 7506 7507 if (class_interface_decl) { 7508 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, 7509 ivar_end = class_interface_decl->ivar_end(); 7510 clang::ObjCInterfaceDecl *superclass_interface_decl = 7511 class_interface_decl->getSuperClass(); 7512 7513 for (ivar_pos = class_interface_decl->ivar_begin(); 7514 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) { 7515 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos; 7516 7517 if (ivar_decl->getName().equals(name_sref)) { 7518 if ((!omit_empty_base_classes && superclass_interface_decl) || 7519 (omit_empty_base_classes && 7520 ObjCDeclHasIVars(superclass_interface_decl, true))) 7521 ++child_idx; 7522 7523 return child_idx; 7524 } 7525 } 7526 7527 if (superclass_interface_decl) { 7528 if (superclass_interface_decl->getName().equals(name_sref)) 7529 return 0; 7530 } 7531 } 7532 } 7533 } 7534 break; 7535 7536 case clang::Type::ObjCObjectPointer: { 7537 CompilerType pointee_clang_type( 7538 getASTContext(), 7539 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()) 7540 ->getPointeeType()); 7541 return pointee_clang_type.GetIndexOfChildWithName( 7542 name, omit_empty_base_classes); 7543 } break; 7544 7545 case clang::Type::ConstantArray: { 7546 // const clang::ConstantArrayType *array = 7547 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr()); 7548 // const uint64_t element_count = 7549 // array->getSize().getLimitedValue(); 7550 // 7551 // if (idx < element_count) 7552 // { 7553 // std::pair<uint64_t, unsigned> field_type_info = 7554 // ast->getTypeInfo(array->getElementType()); 7555 // 7556 // char element_name[32]; 7557 // ::snprintf (element_name, sizeof (element_name), 7558 // "%s[%u]", parent_name ? parent_name : "", idx); 7559 // 7560 // child_name.assign(element_name); 7561 // assert(field_type_info.first % 8 == 0); 7562 // child_byte_size = field_type_info.first / 8; 7563 // child_byte_offset = idx * child_byte_size; 7564 // return array->getElementType().getAsOpaquePtr(); 7565 // } 7566 } break; 7567 7568 // case clang::Type::MemberPointerType: 7569 // { 7570 // MemberPointerType *mem_ptr_type = 7571 // llvm::cast<MemberPointerType>(qual_type.getTypePtr()); 7572 // clang::QualType pointee_type = 7573 // mem_ptr_type->getPointeeType(); 7574 // 7575 // if (ClangASTContext::IsAggregateType 7576 // (pointee_type.getAsOpaquePtr())) 7577 // { 7578 // return GetIndexOfChildWithName (ast, 7579 // mem_ptr_type->getPointeeType().getAsOpaquePtr(), 7580 // name); 7581 // } 7582 // } 7583 // break; 7584 // 7585 case clang::Type::LValueReference: 7586 case clang::Type::RValueReference: { 7587 const clang::ReferenceType *reference_type = 7588 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); 7589 CompilerType pointee_type(getASTContext(), 7590 reference_type->getPointeeType()); 7591 7592 if (pointee_type.IsAggregateType()) { 7593 return pointee_type.GetIndexOfChildWithName(name, 7594 omit_empty_base_classes); 7595 } 7596 } break; 7597 7598 case clang::Type::Pointer: { 7599 const clang::PointerType *pointer_type = 7600 llvm::cast<clang::PointerType>(qual_type.getTypePtr()); 7601 CompilerType pointee_type(getASTContext(), 7602 pointer_type->getPointeeType()); 7603 7604 if (pointee_type.IsAggregateType()) { 7605 return pointee_type.GetIndexOfChildWithName(name, 7606 omit_empty_base_classes); 7607 } else { 7608 // if (parent_name) 7609 // { 7610 // child_name.assign(1, '*'); 7611 // child_name += parent_name; 7612 // } 7613 // 7614 // // We have a pointer to an simple type 7615 // if (idx == 0) 7616 // { 7617 // std::pair<uint64_t, unsigned> clang_type_info 7618 // = ast->getTypeInfo(pointee_type); 7619 // assert(clang_type_info.first % 8 == 0); 7620 // child_byte_size = clang_type_info.first / 8; 7621 // child_byte_offset = 0; 7622 // return pointee_type.getAsOpaquePtr(); 7623 // } 7624 } 7625 } break; 7626 7627 case clang::Type::Auto: 7628 return CompilerType( 7629 getASTContext(), 7630 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) 7631 .GetIndexOfChildWithName(name, omit_empty_base_classes); 7632 7633 case clang::Type::Elaborated: 7634 return CompilerType( 7635 getASTContext(), 7636 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) 7637 .GetIndexOfChildWithName(name, omit_empty_base_classes); 7638 7639 case clang::Type::Paren: 7640 return CompilerType(getASTContext(), 7641 llvm::cast<clang::ParenType>(qual_type)->desugar()) 7642 .GetIndexOfChildWithName(name, omit_empty_base_classes); 7643 7644 case clang::Type::Typedef: 7645 return CompilerType(getASTContext(), 7646 llvm::cast<clang::TypedefType>(qual_type) 7647 ->getDecl() 7648 ->getUnderlyingType()) 7649 .GetIndexOfChildWithName(name, omit_empty_base_classes); 7650 7651 default: 7652 break; 7653 } 7654 } 7655 return UINT32_MAX; 7656} 7657 7658size_t 7659ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) { 7660 if (!type) 7661 return 0; 7662 7663 clang::QualType qual_type(GetCanonicalQualType(type)); 7664 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 7665 switch (type_class) { 7666 case clang::Type::Record: 7667 if (GetCompleteType(type)) { 7668 const clang::CXXRecordDecl *cxx_record_decl = 7669 qual_type->getAsCXXRecordDecl(); 7670 if (cxx_record_decl) { 7671 const clang::ClassTemplateSpecializationDecl *template_decl = 7672 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>( 7673 cxx_record_decl); 7674 if (template_decl) 7675 return template_decl->getTemplateArgs().size(); 7676 } 7677 } 7678 break; 7679 7680 case clang::Type::Typedef: 7681 return (CompilerType(getASTContext(), 7682 llvm::cast<clang::TypedefType>(qual_type) 7683 ->getDecl() 7684 ->getUnderlyingType())) 7685 .GetNumTemplateArguments(); 7686 7687 case clang::Type::Auto: 7688 return (CompilerType( 7689 getASTContext(), 7690 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())) 7691 .GetNumTemplateArguments(); 7692 7693 case clang::Type::Elaborated: 7694 return (CompilerType( 7695 getASTContext(), 7696 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())) 7697 .GetNumTemplateArguments(); 7698 7699 case clang::Type::Paren: 7700 return (CompilerType(getASTContext(), 7701 llvm::cast<clang::ParenType>(qual_type)->desugar())) 7702 .GetNumTemplateArguments(); 7703 7704 default: 7705 break; 7706 } 7707 7708 return 0; 7709} 7710 7711const clang::ClassTemplateSpecializationDecl * 7712ClangASTContext::GetAsTemplateSpecialization( 7713 lldb::opaque_compiler_type_t type) { 7714 if (!type) 7715 return nullptr; 7716 7717 clang::QualType qual_type(GetCanonicalQualType(type)); 7718 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 7719 switch (type_class) { 7720 case clang::Type::Record: { 7721 if (! GetCompleteType(type)) 7722 return nullptr; 7723 const clang::CXXRecordDecl *cxx_record_decl = 7724 qual_type->getAsCXXRecordDecl(); 7725 if (!cxx_record_decl) 7726 return nullptr; 7727 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>( 7728 cxx_record_decl); 7729 } 7730 7731 case clang::Type::Typedef: 7732 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type) 7733 ->getDecl() 7734 ->getUnderlyingType() 7735 .getAsOpaquePtr()); 7736 7737 case clang::Type::Auto: 7738 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type) 7739 ->getDeducedType() 7740 .getAsOpaquePtr()); 7741 7742 case clang::Type::Elaborated: 7743 return GetAsTemplateSpecialization( 7744 llvm::cast<clang::ElaboratedType>(qual_type) 7745 ->getNamedType() 7746 .getAsOpaquePtr()); 7747 7748 case clang::Type::Paren: 7749 return GetAsTemplateSpecialization( 7750 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); 7751 7752 default: 7753 return nullptr; 7754 } 7755} 7756 7757lldb::TemplateArgumentKind 7758ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, 7759 size_t arg_idx) { 7760 const clang::ClassTemplateSpecializationDecl *template_decl = 7761 GetAsTemplateSpecialization(type); 7762 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size()) 7763 return eTemplateArgumentKindNull; 7764 7765 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) { 7766 case clang::TemplateArgument::Null: 7767 return eTemplateArgumentKindNull; 7768 7769 case clang::TemplateArgument::NullPtr: 7770 return eTemplateArgumentKindNullPtr; 7771 7772 case clang::TemplateArgument::Type: 7773 return eTemplateArgumentKindType; 7774 7775 case clang::TemplateArgument::Declaration: 7776 return eTemplateArgumentKindDeclaration; 7777 7778 case clang::TemplateArgument::Integral: 7779 return eTemplateArgumentKindIntegral; 7780 7781 case clang::TemplateArgument::Template: 7782 return eTemplateArgumentKindTemplate; 7783 7784 case clang::TemplateArgument::TemplateExpansion: 7785 return eTemplateArgumentKindTemplateExpansion; 7786 7787 case clang::TemplateArgument::Expression: 7788 return eTemplateArgumentKindExpression; 7789 7790 case clang::TemplateArgument::Pack: 7791 return eTemplateArgumentKindPack; 7792 } 7793 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind"); 7794} 7795 7796CompilerType 7797ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, 7798 size_t idx) { 7799 const clang::ClassTemplateSpecializationDecl *template_decl = 7800 GetAsTemplateSpecialization(type); 7801 if (!template_decl || idx >= template_decl->getTemplateArgs().size()) 7802 return CompilerType(); 7803 7804 const clang::TemplateArgument &template_arg = 7805 template_decl->getTemplateArgs()[idx]; 7806 if (template_arg.getKind() != clang::TemplateArgument::Type) 7807 return CompilerType(); 7808 7809 return CompilerType(getASTContext(), template_arg.getAsType()); 7810} 7811 7812Optional<CompilerType::IntegralTemplateArgument> 7813ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, 7814 size_t idx) { 7815 const clang::ClassTemplateSpecializationDecl *template_decl = 7816 GetAsTemplateSpecialization(type); 7817 if (! template_decl || idx >= template_decl->getTemplateArgs().size()) 7818 return llvm::None; 7819 7820 const clang::TemplateArgument &template_arg = 7821 template_decl->getTemplateArgs()[idx]; 7822 if (template_arg.getKind() != clang::TemplateArgument::Integral) 7823 return llvm::None; 7824 7825 return {{template_arg.getAsIntegral(), 7826 CompilerType(getASTContext(), template_arg.getIntegralType())}}; 7827} 7828 7829CompilerType ClangASTContext::GetTypeForFormatters(void *type) { 7830 if (type) 7831 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type)); 7832 return CompilerType(); 7833} 7834 7835clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) { 7836 const clang::EnumType *enutype = 7837 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type)); 7838 if (enutype) 7839 return enutype->getDecl(); 7840 return NULL; 7841} 7842 7843clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) { 7844 const clang::RecordType *record_type = 7845 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type)); 7846 if (record_type) 7847 return record_type->getDecl(); 7848 return nullptr; 7849} 7850 7851clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) { 7852 return ClangUtil::GetAsTagDecl(type); 7853} 7854 7855clang::TypedefNameDecl * 7856ClangASTContext::GetAsTypedefDecl(const CompilerType &type) { 7857 const clang::TypedefType *typedef_type = 7858 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type)); 7859 if (typedef_type) 7860 return typedef_type->getDecl(); 7861 return nullptr; 7862} 7863 7864clang::CXXRecordDecl * 7865ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) { 7866 return GetCanonicalQualType(type)->getAsCXXRecordDecl(); 7867} 7868 7869clang::ObjCInterfaceDecl * 7870ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) { 7871 const clang::ObjCObjectType *objc_class_type = 7872 llvm::dyn_cast<clang::ObjCObjectType>( 7873 ClangUtil::GetCanonicalQualType(type)); 7874 if (objc_class_type) 7875 return objc_class_type->getInterface(); 7876 return nullptr; 7877} 7878 7879clang::FieldDecl *ClangASTContext::AddFieldToRecordType( 7880 const CompilerType &type, llvm::StringRef name, 7881 const CompilerType &field_clang_type, AccessType access, 7882 uint32_t bitfield_bit_size) { 7883 if (!type.IsValid() || !field_clang_type.IsValid()) 7884 return nullptr; 7885 ClangASTContext *ast = 7886 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem()); 7887 if (!ast) 7888 return nullptr; 7889 clang::ASTContext *clang_ast = ast->getASTContext(); 7890 clang::IdentifierInfo *ident = nullptr; 7891 if (!name.empty()) 7892 ident = &clang_ast->Idents.get(name); 7893 7894 clang::FieldDecl *field = nullptr; 7895 7896 clang::Expr *bit_width = nullptr; 7897 if (bitfield_bit_size != 0) { 7898 llvm::APInt bitfield_bit_size_apint( 7899 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size); 7900 bit_width = new (*clang_ast) 7901 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint, 7902 clang_ast->IntTy, clang::SourceLocation()); 7903 } 7904 7905 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type); 7906 if (record_decl) { 7907 field = clang::FieldDecl::Create( 7908 *clang_ast, record_decl, clang::SourceLocation(), 7909 clang::SourceLocation(), 7910 ident, // Identifier 7911 ClangUtil::GetQualType(field_clang_type), // Field type 7912 nullptr, // TInfo * 7913 bit_width, // BitWidth 7914 false, // Mutable 7915 clang::ICIS_NoInit); // HasInit 7916 7917 if (name.empty()) { 7918 // Determine whether this field corresponds to an anonymous struct or 7919 // union. 7920 if (const clang::TagType *TagT = 7921 field->getType()->getAs<clang::TagType>()) { 7922 if (clang::RecordDecl *Rec = 7923 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl())) 7924 if (!Rec->getDeclName()) { 7925 Rec->setAnonymousStructOrUnion(true); 7926 field->setImplicit(); 7927 } 7928 } 7929 } 7930 7931 if (field) { 7932 field->setAccess( 7933 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access)); 7934 7935 record_decl->addDecl(field); 7936 7937#ifdef LLDB_CONFIGURATION_DEBUG 7938 VerifyDecl(field); 7939#endif 7940 } 7941 } else { 7942 clang::ObjCInterfaceDecl *class_interface_decl = 7943 ast->GetAsObjCInterfaceDecl(type); 7944 7945 if (class_interface_decl) { 7946 const bool is_synthesized = false; 7947 7948 field_clang_type.GetCompleteType(); 7949 7950 field = clang::ObjCIvarDecl::Create( 7951 *clang_ast, class_interface_decl, clang::SourceLocation(), 7952 clang::SourceLocation(), 7953 ident, // Identifier 7954 ClangUtil::GetQualType(field_clang_type), // Field type 7955 nullptr, // TypeSourceInfo * 7956 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width, 7957 is_synthesized); 7958 7959 if (field) { 7960 class_interface_decl->addDecl(field); 7961 7962#ifdef LLDB_CONFIGURATION_DEBUG 7963 VerifyDecl(field); 7964#endif 7965 } 7966 } 7967 } 7968 return field; 7969} 7970 7971void ClangASTContext::BuildIndirectFields(const CompilerType &type) { 7972 if (!type) 7973 return; 7974 7975 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); 7976 if (!ast) 7977 return; 7978 7979 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type); 7980 7981 if (!record_decl) 7982 return; 7983 7984 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector; 7985 7986 IndirectFieldVector indirect_fields; 7987 clang::RecordDecl::field_iterator field_pos; 7988 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end(); 7989 clang::RecordDecl::field_iterator last_field_pos = field_end_pos; 7990 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; 7991 last_field_pos = field_pos++) { 7992 if (field_pos->isAnonymousStructOrUnion()) { 7993 clang::QualType field_qual_type = field_pos->getType(); 7994 7995 const clang::RecordType *field_record_type = 7996 field_qual_type->getAs<clang::RecordType>(); 7997 7998 if (!field_record_type) 7999 continue; 8000 8001 clang::RecordDecl *field_record_decl = field_record_type->getDecl(); 8002 8003 if (!field_record_decl) 8004 continue; 8005 8006 for (clang::RecordDecl::decl_iterator 8007 di = field_record_decl->decls_begin(), 8008 de = field_record_decl->decls_end(); 8009 di != de; ++di) { 8010 if (clang::FieldDecl *nested_field_decl = 8011 llvm::dyn_cast<clang::FieldDecl>(*di)) { 8012 clang::NamedDecl **chain = 8013 new (*ast->getASTContext()) clang::NamedDecl *[2]; 8014 chain[0] = *field_pos; 8015 chain[1] = nested_field_decl; 8016 clang::IndirectFieldDecl *indirect_field = 8017 clang::IndirectFieldDecl::Create( 8018 *ast->getASTContext(), record_decl, clang::SourceLocation(), 8019 nested_field_decl->getIdentifier(), 8020 nested_field_decl->getType(), {chain, 2}); 8021 8022 indirect_field->setImplicit(); 8023 8024 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers( 8025 field_pos->getAccess(), nested_field_decl->getAccess())); 8026 8027 indirect_fields.push_back(indirect_field); 8028 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl = 8029 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) { 8030 size_t nested_chain_size = 8031 nested_indirect_field_decl->getChainingSize(); 8032 clang::NamedDecl **chain = new (*ast->getASTContext()) 8033 clang::NamedDecl *[nested_chain_size + 1]; 8034 chain[0] = *field_pos; 8035 8036 int chain_index = 1; 8037 for (clang::IndirectFieldDecl::chain_iterator 8038 nci = nested_indirect_field_decl->chain_begin(), 8039 nce = nested_indirect_field_decl->chain_end(); 8040 nci < nce; ++nci) { 8041 chain[chain_index] = *nci; 8042 chain_index++; 8043 } 8044 8045 clang::IndirectFieldDecl *indirect_field = 8046 clang::IndirectFieldDecl::Create( 8047 *ast->getASTContext(), record_decl, clang::SourceLocation(), 8048 nested_indirect_field_decl->getIdentifier(), 8049 nested_indirect_field_decl->getType(), 8050 {chain, nested_chain_size + 1}); 8051 8052 indirect_field->setImplicit(); 8053 8054 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers( 8055 field_pos->getAccess(), nested_indirect_field_decl->getAccess())); 8056 8057 indirect_fields.push_back(indirect_field); 8058 } 8059 } 8060 } 8061 } 8062 8063 // Check the last field to see if it has an incomplete array type as its last 8064 // member and if it does, the tell the record decl about it 8065 if (last_field_pos != field_end_pos) { 8066 if (last_field_pos->getType()->isIncompleteArrayType()) 8067 record_decl->hasFlexibleArrayMember(); 8068 } 8069 8070 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), 8071 ife = indirect_fields.end(); 8072 ifi < ife; ++ifi) { 8073 record_decl->addDecl(*ifi); 8074 } 8075} 8076 8077void ClangASTContext::SetIsPacked(const CompilerType &type) { 8078 if (type) { 8079 ClangASTContext *ast = 8080 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); 8081 if (ast) { 8082 clang::RecordDecl *record_decl = GetAsRecordDecl(type); 8083 8084 if (!record_decl) 8085 return; 8086 8087 record_decl->addAttr( 8088 clang::PackedAttr::CreateImplicit(*ast->getASTContext())); 8089 } 8090 } 8091} 8092 8093clang::VarDecl *ClangASTContext::AddVariableToRecordType( 8094 const CompilerType &type, llvm::StringRef name, 8095 const CompilerType &var_type, AccessType access) { 8096 if (!type.IsValid() || !var_type.IsValid()) 8097 return nullptr; 8098 8099 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); 8100 if (!ast) 8101 return nullptr; 8102 8103 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type); 8104 if (!record_decl) 8105 return nullptr; 8106 8107 clang::VarDecl *var_decl = nullptr; 8108 clang::IdentifierInfo *ident = nullptr; 8109 if (!name.empty()) 8110 ident = &ast->getASTContext()->Idents.get(name); 8111 8112 var_decl = clang::VarDecl::Create( 8113 *ast->getASTContext(), // ASTContext & 8114 record_decl, // DeclContext * 8115 clang::SourceLocation(), // clang::SourceLocation StartLoc 8116 clang::SourceLocation(), // clang::SourceLocation IdLoc 8117 ident, // clang::IdentifierInfo * 8118 ClangUtil::GetQualType(var_type), // Variable clang::QualType 8119 nullptr, // TypeSourceInfo * 8120 clang::SC_Static); // StorageClass 8121 if (!var_decl) 8122 return nullptr; 8123 8124 var_decl->setAccess( 8125 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access)); 8126 record_decl->addDecl(var_decl); 8127 8128#ifdef LLDB_CONFIGURATION_DEBUG 8129 VerifyDecl(var_decl); 8130#endif 8131 8132 return var_decl; 8133} 8134 8135clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType( 8136 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name, 8137 const CompilerType &method_clang_type, lldb::AccessType access, 8138 bool is_virtual, bool is_static, bool is_inline, bool is_explicit, 8139 bool is_attr_used, bool is_artificial) { 8140 if (!type || !method_clang_type.IsValid() || name == nullptr || 8141 name[0] == '\0') 8142 return nullptr; 8143 8144 clang::QualType record_qual_type(GetCanonicalQualType(type)); 8145 8146 clang::CXXRecordDecl *cxx_record_decl = 8147 record_qual_type->getAsCXXRecordDecl(); 8148 8149 if (cxx_record_decl == nullptr) 8150 return nullptr; 8151 8152 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type)); 8153 8154 clang::CXXMethodDecl *cxx_method_decl = nullptr; 8155 8156 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name)); 8157 8158 const clang::FunctionType *function_type = 8159 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr()); 8160 8161 if (function_type == nullptr) 8162 return nullptr; 8163 8164 const clang::FunctionProtoType *method_function_prototype( 8165 llvm::dyn_cast<clang::FunctionProtoType>(function_type)); 8166 8167 if (!method_function_prototype) 8168 return nullptr; 8169 8170 unsigned int num_params = method_function_prototype->getNumParams(); 8171 8172 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr); 8173 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr); 8174 8175 if (is_artificial) 8176 return nullptr; // skip everything artificial 8177 8178 if (name[0] == '~') { 8179 cxx_dtor_decl = clang::CXXDestructorDecl::Create( 8180 *getASTContext(), cxx_record_decl, clang::SourceLocation(), 8181 clang::DeclarationNameInfo( 8182 getASTContext()->DeclarationNames.getCXXDestructorName( 8183 getASTContext()->getCanonicalType(record_qual_type)), 8184 clang::SourceLocation()), 8185 method_qual_type, nullptr, is_inline, is_artificial); 8186 cxx_method_decl = cxx_dtor_decl; 8187 } else if (decl_name == cxx_record_decl->getDeclName()) { 8188 cxx_ctor_decl = clang::CXXConstructorDecl::Create( 8189 *getASTContext(), cxx_record_decl, clang::SourceLocation(), 8190 clang::DeclarationNameInfo( 8191 getASTContext()->DeclarationNames.getCXXConstructorName( 8192 getASTContext()->getCanonicalType(record_qual_type)), 8193 clang::SourceLocation()), 8194 method_qual_type, 8195 nullptr, // TypeSourceInfo * 8196 is_explicit, is_inline, is_artificial, false /*is_constexpr*/); 8197 cxx_method_decl = cxx_ctor_decl; 8198 } else { 8199 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None; 8200 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS; 8201 8202 if (IsOperator(name, op_kind)) { 8203 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) { 8204 // Check the number of operator parameters. Sometimes we have seen bad 8205 // DWARF that doesn't correctly describe operators and if we try to 8206 // create a method and add it to the class, clang will assert and 8207 // crash, so we need to make sure things are acceptable. 8208 const bool is_method = true; 8209 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount( 8210 is_method, op_kind, num_params)) 8211 return nullptr; 8212 cxx_method_decl = clang::CXXMethodDecl::Create( 8213 *getASTContext(), cxx_record_decl, clang::SourceLocation(), 8214 clang::DeclarationNameInfo( 8215 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind), 8216 clang::SourceLocation()), 8217 method_qual_type, 8218 nullptr, // TypeSourceInfo * 8219 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation()); 8220 } else if (num_params == 0) { 8221 // Conversion operators don't take params... 8222 cxx_method_decl = clang::CXXConversionDecl::Create( 8223 *getASTContext(), cxx_record_decl, clang::SourceLocation(), 8224 clang::DeclarationNameInfo( 8225 getASTContext()->DeclarationNames.getCXXConversionFunctionName( 8226 getASTContext()->getCanonicalType( 8227 function_type->getReturnType())), 8228 clang::SourceLocation()), 8229 method_qual_type, 8230 nullptr, // TypeSourceInfo * 8231 is_inline, is_explicit, false /*is_constexpr*/, 8232 clang::SourceLocation()); 8233 } 8234 } 8235 8236 if (cxx_method_decl == nullptr) { 8237 cxx_method_decl = clang::CXXMethodDecl::Create( 8238 *getASTContext(), cxx_record_decl, clang::SourceLocation(), 8239 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()), 8240 method_qual_type, 8241 nullptr, // TypeSourceInfo * 8242 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation()); 8243 } 8244 } 8245 8246 clang::AccessSpecifier access_specifier = 8247 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access); 8248 8249 cxx_method_decl->setAccess(access_specifier); 8250 cxx_method_decl->setVirtualAsWritten(is_virtual); 8251 8252 if (is_attr_used) 8253 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext())); 8254 8255 if (mangled_name != NULL) { 8256 cxx_method_decl->addAttr( 8257 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name)); 8258 } 8259 8260 // Populate the method decl with parameter decls 8261 8262 llvm::SmallVector<clang::ParmVarDecl *, 12> params; 8263 8264 for (unsigned param_index = 0; param_index < num_params; ++param_index) { 8265 params.push_back(clang::ParmVarDecl::Create( 8266 *getASTContext(), cxx_method_decl, clang::SourceLocation(), 8267 clang::SourceLocation(), 8268 nullptr, // anonymous 8269 method_function_prototype->getParamType(param_index), nullptr, 8270 clang::SC_None, nullptr)); 8271 } 8272 8273 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params)); 8274 8275 cxx_record_decl->addDecl(cxx_method_decl); 8276 8277 // Sometimes the debug info will mention a constructor (default/copy/move), 8278 // destructor, or assignment operator (copy/move) but there won't be any 8279 // version of this in the code. So we check if the function was artificially 8280 // generated and if it is trivial and this lets the compiler/backend know 8281 // that it can inline the IR for these when it needs to and we can avoid a 8282 // "missing function" error when running expressions. 8283 8284 if (is_artificial) { 8285 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() && 8286 cxx_record_decl->hasTrivialDefaultConstructor()) || 8287 (cxx_ctor_decl->isCopyConstructor() && 8288 cxx_record_decl->hasTrivialCopyConstructor()) || 8289 (cxx_ctor_decl->isMoveConstructor() && 8290 cxx_record_decl->hasTrivialMoveConstructor()))) { 8291 cxx_ctor_decl->setDefaulted(); 8292 cxx_ctor_decl->setTrivial(true); 8293 } else if (cxx_dtor_decl) { 8294 if (cxx_record_decl->hasTrivialDestructor()) { 8295 cxx_dtor_decl->setDefaulted(); 8296 cxx_dtor_decl->setTrivial(true); 8297 } 8298 } else if ((cxx_method_decl->isCopyAssignmentOperator() && 8299 cxx_record_decl->hasTrivialCopyAssignment()) || 8300 (cxx_method_decl->isMoveAssignmentOperator() && 8301 cxx_record_decl->hasTrivialMoveAssignment())) { 8302 cxx_method_decl->setDefaulted(); 8303 cxx_method_decl->setTrivial(true); 8304 } 8305 } 8306 8307#ifdef LLDB_CONFIGURATION_DEBUG 8308 VerifyDecl(cxx_method_decl); 8309#endif 8310 8311 // printf ("decl->isPolymorphic() = %i\n", 8312 // cxx_record_decl->isPolymorphic()); 8313 // printf ("decl->isAggregate() = %i\n", 8314 // cxx_record_decl->isAggregate()); 8315 // printf ("decl->isPOD() = %i\n", 8316 // cxx_record_decl->isPOD()); 8317 // printf ("decl->isEmpty() = %i\n", 8318 // cxx_record_decl->isEmpty()); 8319 // printf ("decl->isAbstract() = %i\n", 8320 // cxx_record_decl->isAbstract()); 8321 // printf ("decl->hasTrivialConstructor() = %i\n", 8322 // cxx_record_decl->hasTrivialConstructor()); 8323 // printf ("decl->hasTrivialCopyConstructor() = %i\n", 8324 // cxx_record_decl->hasTrivialCopyConstructor()); 8325 // printf ("decl->hasTrivialCopyAssignment() = %i\n", 8326 // cxx_record_decl->hasTrivialCopyAssignment()); 8327 // printf ("decl->hasTrivialDestructor() = %i\n", 8328 // cxx_record_decl->hasTrivialDestructor()); 8329 return cxx_method_decl; 8330} 8331 8332void ClangASTContext::AddMethodOverridesForCXXRecordType( 8333 lldb::opaque_compiler_type_t type) { 8334 if (auto *record = GetAsCXXRecordDecl(type)) 8335 for (auto *method : record->methods()) 8336 addOverridesForMethod(method); 8337} 8338 8339#pragma mark C++ Base Classes 8340 8341std::unique_ptr<clang::CXXBaseSpecifier> 8342ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type, 8343 AccessType access, bool is_virtual, 8344 bool base_of_class) { 8345 if (!type) 8346 return nullptr; 8347 8348 return llvm::make_unique<clang::CXXBaseSpecifier>( 8349 clang::SourceRange(), is_virtual, base_of_class, 8350 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access), 8351 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)), 8352 clang::SourceLocation()); 8353} 8354 8355bool ClangASTContext::TransferBaseClasses( 8356 lldb::opaque_compiler_type_t type, 8357 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) { 8358 if (!type) 8359 return false; 8360 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type); 8361 if (!cxx_record_decl) 8362 return false; 8363 std::vector<clang::CXXBaseSpecifier *> raw_bases; 8364 raw_bases.reserve(bases.size()); 8365 8366 // Clang will make a copy of them, so it's ok that we pass pointers that we're 8367 // about to destroy. 8368 for (auto &b : bases) 8369 raw_bases.push_back(b.get()); 8370 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size()); 8371 return true; 8372} 8373 8374bool ClangASTContext::SetObjCSuperClass( 8375 const CompilerType &type, const CompilerType &superclass_clang_type) { 8376 ClangASTContext *ast = 8377 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem()); 8378 if (!ast) 8379 return false; 8380 clang::ASTContext *clang_ast = ast->getASTContext(); 8381 8382 if (type && superclass_clang_type.IsValid() && 8383 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) { 8384 clang::ObjCInterfaceDecl *class_interface_decl = 8385 GetAsObjCInterfaceDecl(type); 8386 clang::ObjCInterfaceDecl *super_interface_decl = 8387 GetAsObjCInterfaceDecl(superclass_clang_type); 8388 if (class_interface_decl && super_interface_decl) { 8389 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo( 8390 clang_ast->getObjCInterfaceType(super_interface_decl))); 8391 return true; 8392 } 8393 } 8394 return false; 8395} 8396 8397bool ClangASTContext::AddObjCClassProperty( 8398 const CompilerType &type, const char *property_name, 8399 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl, 8400 const char *property_setter_name, const char *property_getter_name, 8401 uint32_t property_attributes, ClangASTMetadata *metadata) { 8402 if (!type || !property_clang_type.IsValid() || property_name == nullptr || 8403 property_name[0] == '\0') 8404 return false; 8405 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); 8406 if (!ast) 8407 return false; 8408 clang::ASTContext *clang_ast = ast->getASTContext(); 8409 8410 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type); 8411 8412 if (class_interface_decl) { 8413 CompilerType property_clang_type_to_access; 8414 8415 if (property_clang_type.IsValid()) 8416 property_clang_type_to_access = property_clang_type; 8417 else if (ivar_decl) 8418 property_clang_type_to_access = 8419 CompilerType(clang_ast, ivar_decl->getType()); 8420 8421 if (class_interface_decl && property_clang_type_to_access.IsValid()) { 8422 clang::TypeSourceInfo *prop_type_source; 8423 if (ivar_decl) 8424 prop_type_source = 8425 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType()); 8426 else 8427 prop_type_source = clang_ast->getTrivialTypeSourceInfo( 8428 ClangUtil::GetQualType(property_clang_type)); 8429 8430 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create( 8431 *clang_ast, class_interface_decl, 8432 clang::SourceLocation(), // Source Location 8433 &clang_ast->Idents.get(property_name), 8434 clang::SourceLocation(), // Source Location for AT 8435 clang::SourceLocation(), // Source location for ( 8436 ivar_decl ? ivar_decl->getType() 8437 : ClangUtil::GetQualType(property_clang_type), 8438 prop_type_source); 8439 8440 if (property_decl) { 8441 if (metadata) 8442 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata); 8443 8444 class_interface_decl->addDecl(property_decl); 8445 8446 clang::Selector setter_sel, getter_sel; 8447 8448 if (property_setter_name != nullptr) { 8449 std::string property_setter_no_colon( 8450 property_setter_name, strlen(property_setter_name) - 1); 8451 clang::IdentifierInfo *setter_ident = 8452 &clang_ast->Idents.get(property_setter_no_colon); 8453 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident); 8454 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) { 8455 std::string setter_sel_string("set"); 8456 setter_sel_string.push_back(::toupper(property_name[0])); 8457 setter_sel_string.append(&property_name[1]); 8458 clang::IdentifierInfo *setter_ident = 8459 &clang_ast->Idents.get(setter_sel_string); 8460 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident); 8461 } 8462 property_decl->setSetterName(setter_sel); 8463 property_decl->setPropertyAttributes( 8464 clang::ObjCPropertyDecl::OBJC_PR_setter); 8465 8466 if (property_getter_name != nullptr) { 8467 clang::IdentifierInfo *getter_ident = 8468 &clang_ast->Idents.get(property_getter_name); 8469 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident); 8470 } else { 8471 clang::IdentifierInfo *getter_ident = 8472 &clang_ast->Idents.get(property_name); 8473 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident); 8474 } 8475 property_decl->setGetterName(getter_sel); 8476 property_decl->setPropertyAttributes( 8477 clang::ObjCPropertyDecl::OBJC_PR_getter); 8478 8479 if (ivar_decl) 8480 property_decl->setPropertyIvarDecl(ivar_decl); 8481 8482 if (property_attributes & DW_APPLE_PROPERTY_readonly) 8483 property_decl->setPropertyAttributes( 8484 clang::ObjCPropertyDecl::OBJC_PR_readonly); 8485 if (property_attributes & DW_APPLE_PROPERTY_readwrite) 8486 property_decl->setPropertyAttributes( 8487 clang::ObjCPropertyDecl::OBJC_PR_readwrite); 8488 if (property_attributes & DW_APPLE_PROPERTY_assign) 8489 property_decl->setPropertyAttributes( 8490 clang::ObjCPropertyDecl::OBJC_PR_assign); 8491 if (property_attributes & DW_APPLE_PROPERTY_retain) 8492 property_decl->setPropertyAttributes( 8493 clang::ObjCPropertyDecl::OBJC_PR_retain); 8494 if (property_attributes & DW_APPLE_PROPERTY_copy) 8495 property_decl->setPropertyAttributes( 8496 clang::ObjCPropertyDecl::OBJC_PR_copy); 8497 if (property_attributes & DW_APPLE_PROPERTY_nonatomic) 8498 property_decl->setPropertyAttributes( 8499 clang::ObjCPropertyDecl::OBJC_PR_nonatomic); 8500 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability) 8501 property_decl->setPropertyAttributes( 8502 clang::ObjCPropertyDecl::OBJC_PR_nullability); 8503 if (property_attributes & 8504 clang::ObjCPropertyDecl::OBJC_PR_null_resettable) 8505 property_decl->setPropertyAttributes( 8506 clang::ObjCPropertyDecl::OBJC_PR_null_resettable); 8507 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) 8508 property_decl->setPropertyAttributes( 8509 clang::ObjCPropertyDecl::OBJC_PR_class); 8510 8511 const bool isInstance = 8512 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0; 8513 8514 if (!getter_sel.isNull() && 8515 !(isInstance 8516 ? class_interface_decl->lookupInstanceMethod(getter_sel) 8517 : class_interface_decl->lookupClassMethod(getter_sel))) { 8518 const bool isVariadic = false; 8519 const bool isSynthesized = false; 8520 const bool isImplicitlyDeclared = true; 8521 const bool isDefined = false; 8522 const clang::ObjCMethodDecl::ImplementationControl impControl = 8523 clang::ObjCMethodDecl::None; 8524 const bool HasRelatedResultType = false; 8525 8526 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create( 8527 *clang_ast, clang::SourceLocation(), clang::SourceLocation(), 8528 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access), 8529 nullptr, class_interface_decl, isInstance, isVariadic, 8530 isSynthesized, isImplicitlyDeclared, isDefined, impControl, 8531 HasRelatedResultType); 8532 8533 if (getter && metadata) 8534 ClangASTContext::SetMetadata(clang_ast, getter, *metadata); 8535 8536 if (getter) { 8537 getter->setMethodParams(*clang_ast, 8538 llvm::ArrayRef<clang::ParmVarDecl *>(), 8539 llvm::ArrayRef<clang::SourceLocation>()); 8540 8541 class_interface_decl->addDecl(getter); 8542 } 8543 } 8544 8545 if (!setter_sel.isNull() && 8546 !(isInstance 8547 ? class_interface_decl->lookupInstanceMethod(setter_sel) 8548 : class_interface_decl->lookupClassMethod(setter_sel))) { 8549 clang::QualType result_type = clang_ast->VoidTy; 8550 const bool isVariadic = false; 8551 const bool isSynthesized = false; 8552 const bool isImplicitlyDeclared = true; 8553 const bool isDefined = false; 8554 const clang::ObjCMethodDecl::ImplementationControl impControl = 8555 clang::ObjCMethodDecl::None; 8556 const bool HasRelatedResultType = false; 8557 8558 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create( 8559 *clang_ast, clang::SourceLocation(), clang::SourceLocation(), 8560 setter_sel, result_type, nullptr, class_interface_decl, 8561 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared, 8562 isDefined, impControl, HasRelatedResultType); 8563 8564 if (setter && metadata) 8565 ClangASTContext::SetMetadata(clang_ast, setter, *metadata); 8566 8567 llvm::SmallVector<clang::ParmVarDecl *, 1> params; 8568 8569 params.push_back(clang::ParmVarDecl::Create( 8570 *clang_ast, setter, clang::SourceLocation(), 8571 clang::SourceLocation(), 8572 nullptr, // anonymous 8573 ClangUtil::GetQualType(property_clang_type_to_access), nullptr, 8574 clang::SC_Auto, nullptr)); 8575 8576 if (setter) { 8577 setter->setMethodParams( 8578 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params), 8579 llvm::ArrayRef<clang::SourceLocation>()); 8580 8581 class_interface_decl->addDecl(setter); 8582 } 8583 } 8584 8585 return true; 8586 } 8587 } 8588 } 8589 return false; 8590} 8591 8592bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type, 8593 bool check_superclass) { 8594 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type); 8595 if (class_interface_decl) 8596 return ObjCDeclHasIVars(class_interface_decl, check_superclass); 8597 return false; 8598} 8599 8600clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType( 8601 const CompilerType &type, 8602 const char *name, // the full symbol name as seen in the symbol table 8603 // (lldb::opaque_compiler_type_t type, "-[NString 8604 // stringWithCString:]") 8605 const CompilerType &method_clang_type, lldb::AccessType access, 8606 bool is_artificial, bool is_variadic) { 8607 if (!type || !method_clang_type.IsValid()) 8608 return nullptr; 8609 8610 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type); 8611 8612 if (class_interface_decl == nullptr) 8613 return nullptr; 8614 ClangASTContext *lldb_ast = 8615 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); 8616 if (lldb_ast == nullptr) 8617 return nullptr; 8618 clang::ASTContext *ast = lldb_ast->getASTContext(); 8619 8620 const char *selector_start = ::strchr(name, ' '); 8621 if (selector_start == nullptr) 8622 return nullptr; 8623 8624 selector_start++; 8625 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents; 8626 8627 size_t len = 0; 8628 const char *start; 8629 // printf ("name = '%s'\n", name); 8630 8631 unsigned num_selectors_with_args = 0; 8632 for (start = selector_start; start && *start != '\0' && *start != ']'; 8633 start += len) { 8634 len = ::strcspn(start, ":]"); 8635 bool has_arg = (start[len] == ':'); 8636 if (has_arg) 8637 ++num_selectors_with_args; 8638 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len))); 8639 if (has_arg) 8640 len += 1; 8641 } 8642 8643 if (selector_idents.size() == 0) 8644 return nullptr; 8645 8646 clang::Selector method_selector = ast->Selectors.getSelector( 8647 num_selectors_with_args ? selector_idents.size() : 0, 8648 selector_idents.data()); 8649 8650 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type)); 8651 8652 // Populate the method decl with parameter decls 8653 const clang::Type *method_type(method_qual_type.getTypePtr()); 8654 8655 if (method_type == nullptr) 8656 return nullptr; 8657 8658 const clang::FunctionProtoType *method_function_prototype( 8659 llvm::dyn_cast<clang::FunctionProtoType>(method_type)); 8660 8661 if (!method_function_prototype) 8662 return nullptr; 8663 8664 bool is_synthesized = false; 8665 bool is_defined = false; 8666 clang::ObjCMethodDecl::ImplementationControl imp_control = 8667 clang::ObjCMethodDecl::None; 8668 8669 const unsigned num_args = method_function_prototype->getNumParams(); 8670 8671 if (num_args != num_selectors_with_args) 8672 return nullptr; // some debug information is corrupt. We are not going to 8673 // deal with it. 8674 8675 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create( 8676 *ast, 8677 clang::SourceLocation(), // beginLoc, 8678 clang::SourceLocation(), // endLoc, 8679 method_selector, method_function_prototype->getReturnType(), 8680 nullptr, // TypeSourceInfo *ResultTInfo, 8681 ClangASTContext::GetASTContext(ast)->GetDeclContextForType( 8682 ClangUtil::GetQualType(type)), 8683 name[0] == '-', is_variadic, is_synthesized, 8684 true, // is_implicitly_declared; we force this to true because we don't 8685 // have source locations 8686 is_defined, imp_control, false /*has_related_result_type*/); 8687 8688 if (objc_method_decl == nullptr) 8689 return nullptr; 8690 8691 if (num_args > 0) { 8692 llvm::SmallVector<clang::ParmVarDecl *, 12> params; 8693 8694 for (unsigned param_index = 0; param_index < num_args; ++param_index) { 8695 params.push_back(clang::ParmVarDecl::Create( 8696 *ast, objc_method_decl, clang::SourceLocation(), 8697 clang::SourceLocation(), 8698 nullptr, // anonymous 8699 method_function_prototype->getParamType(param_index), nullptr, 8700 clang::SC_Auto, nullptr)); 8701 } 8702 8703 objc_method_decl->setMethodParams( 8704 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params), 8705 llvm::ArrayRef<clang::SourceLocation>()); 8706 } 8707 8708 class_interface_decl->addDecl(objc_method_decl); 8709 8710#ifdef LLDB_CONFIGURATION_DEBUG 8711 VerifyDecl(objc_method_decl); 8712#endif 8713 8714 return objc_method_decl; 8715} 8716 8717bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) { 8718 if (ClangUtil::IsClangType(type)) 8719 return false; 8720 8721 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); 8722 8723 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 8724 switch (type_class) { 8725 case clang::Type::Record: { 8726 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 8727 if (cxx_record_decl) 8728 return cxx_record_decl->hasExternalLexicalStorage() || 8729 cxx_record_decl->hasExternalVisibleStorage(); 8730 } break; 8731 8732 case clang::Type::Enum: { 8733 clang::EnumDecl *enum_decl = 8734 llvm::cast<clang::EnumType>(qual_type)->getDecl(); 8735 if (enum_decl) 8736 return enum_decl->hasExternalLexicalStorage() || 8737 enum_decl->hasExternalVisibleStorage(); 8738 } break; 8739 8740 case clang::Type::ObjCObject: 8741 case clang::Type::ObjCInterface: { 8742 const clang::ObjCObjectType *objc_class_type = 8743 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); 8744 assert(objc_class_type); 8745 if (objc_class_type) { 8746 clang::ObjCInterfaceDecl *class_interface_decl = 8747 objc_class_type->getInterface(); 8748 8749 if (class_interface_decl) 8750 return class_interface_decl->hasExternalLexicalStorage() || 8751 class_interface_decl->hasExternalVisibleStorage(); 8752 } 8753 } break; 8754 8755 case clang::Type::Typedef: 8756 return GetHasExternalStorage(CompilerType( 8757 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type) 8758 ->getDecl() 8759 ->getUnderlyingType() 8760 .getAsOpaquePtr())); 8761 8762 case clang::Type::Auto: 8763 return GetHasExternalStorage(CompilerType( 8764 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type) 8765 ->getDeducedType() 8766 .getAsOpaquePtr())); 8767 8768 case clang::Type::Elaborated: 8769 return GetHasExternalStorage(CompilerType( 8770 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type) 8771 ->getNamedType() 8772 .getAsOpaquePtr())); 8773 8774 case clang::Type::Paren: 8775 return GetHasExternalStorage(CompilerType( 8776 type.GetTypeSystem(), 8777 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())); 8778 8779 default: 8780 break; 8781 } 8782 return false; 8783} 8784 8785bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type, 8786 bool has_extern) { 8787 if (!type) 8788 return false; 8789 8790 clang::QualType qual_type(GetCanonicalQualType(type)); 8791 8792 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 8793 switch (type_class) { 8794 case clang::Type::Record: { 8795 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 8796 if (cxx_record_decl) { 8797 cxx_record_decl->setHasExternalLexicalStorage(has_extern); 8798 cxx_record_decl->setHasExternalVisibleStorage(has_extern); 8799 return true; 8800 } 8801 } break; 8802 8803 case clang::Type::Enum: { 8804 clang::EnumDecl *enum_decl = 8805 llvm::cast<clang::EnumType>(qual_type)->getDecl(); 8806 if (enum_decl) { 8807 enum_decl->setHasExternalLexicalStorage(has_extern); 8808 enum_decl->setHasExternalVisibleStorage(has_extern); 8809 return true; 8810 } 8811 } break; 8812 8813 case clang::Type::ObjCObject: 8814 case clang::Type::ObjCInterface: { 8815 const clang::ObjCObjectType *objc_class_type = 8816 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); 8817 assert(objc_class_type); 8818 if (objc_class_type) { 8819 clang::ObjCInterfaceDecl *class_interface_decl = 8820 objc_class_type->getInterface(); 8821 8822 if (class_interface_decl) { 8823 class_interface_decl->setHasExternalLexicalStorage(has_extern); 8824 class_interface_decl->setHasExternalVisibleStorage(has_extern); 8825 return true; 8826 } 8827 } 8828 } break; 8829 8830 case clang::Type::Typedef: 8831 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type) 8832 ->getDecl() 8833 ->getUnderlyingType() 8834 .getAsOpaquePtr(), 8835 has_extern); 8836 8837 case clang::Type::Auto: 8838 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type) 8839 ->getDeducedType() 8840 .getAsOpaquePtr(), 8841 has_extern); 8842 8843 case clang::Type::Elaborated: 8844 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type) 8845 ->getNamedType() 8846 .getAsOpaquePtr(), 8847 has_extern); 8848 8849 case clang::Type::Paren: 8850 return SetHasExternalStorage( 8851 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), 8852 has_extern); 8853 8854 default: 8855 break; 8856 } 8857 return false; 8858} 8859 8860#pragma mark TagDecl 8861 8862bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) { 8863 clang::QualType qual_type(ClangUtil::GetQualType(type)); 8864 if (!qual_type.isNull()) { 8865 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>(); 8866 if (tag_type) { 8867 clang::TagDecl *tag_decl = tag_type->getDecl(); 8868 if (tag_decl) { 8869 tag_decl->startDefinition(); 8870 return true; 8871 } 8872 } 8873 8874 const clang::ObjCObjectType *object_type = 8875 qual_type->getAs<clang::ObjCObjectType>(); 8876 if (object_type) { 8877 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface(); 8878 if (interface_decl) { 8879 interface_decl->startDefinition(); 8880 return true; 8881 } 8882 } 8883 } 8884 return false; 8885} 8886 8887bool ClangASTContext::CompleteTagDeclarationDefinition( 8888 const CompilerType &type) { 8889 clang::QualType qual_type(ClangUtil::GetQualType(type)); 8890 if (!qual_type.isNull()) { 8891 // Make sure we use the same methodology as 8892 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end 8893 // the definition. Previously we were calling 8894 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>(); 8895 if (tag_type) { 8896 clang::TagDecl *tag_decl = tag_type->getDecl(); 8897 if (tag_decl) { 8898 clang::CXXRecordDecl *cxx_record_decl = 8899 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl); 8900 8901 if (cxx_record_decl) { 8902 if (!cxx_record_decl->isCompleteDefinition()) 8903 cxx_record_decl->completeDefinition(); 8904 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true); 8905 cxx_record_decl->setHasExternalLexicalStorage(false); 8906 cxx_record_decl->setHasExternalVisibleStorage(false); 8907 return true; 8908 } 8909 } 8910 } 8911 8912 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>(); 8913 8914 if (enutype) { 8915 clang::EnumDecl *enum_decl = enutype->getDecl(); 8916 8917 if (enum_decl) { 8918 if (!enum_decl->isCompleteDefinition()) { 8919 ClangASTContext *lldb_ast = 8920 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); 8921 if (lldb_ast == nullptr) 8922 return false; 8923 clang::ASTContext *ast = lldb_ast->getASTContext(); 8924 8925 /// TODO This really needs to be fixed. 8926 8927 QualType integer_type(enum_decl->getIntegerType()); 8928 if (!integer_type.isNull()) { 8929 unsigned NumPositiveBits = 1; 8930 unsigned NumNegativeBits = 0; 8931 8932 clang::QualType promotion_qual_type; 8933 // If the enum integer type is less than an integer in bit width, 8934 // then we must promote it to an integer size. 8935 if (ast->getTypeSize(enum_decl->getIntegerType()) < 8936 ast->getTypeSize(ast->IntTy)) { 8937 if (enum_decl->getIntegerType()->isSignedIntegerType()) 8938 promotion_qual_type = ast->IntTy; 8939 else 8940 promotion_qual_type = ast->UnsignedIntTy; 8941 } else 8942 promotion_qual_type = enum_decl->getIntegerType(); 8943 8944 enum_decl->completeDefinition(enum_decl->getIntegerType(), 8945 promotion_qual_type, NumPositiveBits, 8946 NumNegativeBits); 8947 } 8948 } 8949 return true; 8950 } 8951 } 8952 } 8953 return false; 8954} 8955 8956clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType( 8957 const CompilerType &enum_type, const Declaration &decl, const char *name, 8958 const llvm::APSInt &value) { 8959 8960 if (!enum_type || ConstString(name).IsEmpty()) 8961 return nullptr; 8962 8963 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this)); 8964 8965 lldb::opaque_compiler_type_t enum_opaque_compiler_type = 8966 enum_type.GetOpaqueQualType(); 8967 8968 if (!enum_opaque_compiler_type) 8969 return nullptr; 8970 8971 clang::QualType enum_qual_type( 8972 GetCanonicalQualType(enum_opaque_compiler_type)); 8973 8974 const clang::Type *clang_type = enum_qual_type.getTypePtr(); 8975 8976 if (!clang_type) 8977 return nullptr; 8978 8979 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type); 8980 8981 if (!enutype) 8982 return nullptr; 8983 8984 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create( 8985 *getASTContext(), enutype->getDecl(), clang::SourceLocation(), 8986 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier 8987 clang::QualType(enutype, 0), nullptr, value); 8988 8989 if (!enumerator_decl) 8990 return nullptr; 8991 8992 enutype->getDecl()->addDecl(enumerator_decl); 8993 8994#ifdef LLDB_CONFIGURATION_DEBUG 8995 VerifyDecl(enumerator_decl); 8996#endif 8997 8998 return enumerator_decl; 8999} 9000 9001clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType( 9002 const CompilerType &enum_type, const Declaration &decl, const char *name, 9003 int64_t enum_value, uint32_t enum_value_bit_size) { 9004 CompilerType underlying_type = 9005 GetEnumerationIntegerType(enum_type.GetOpaqueQualType()); 9006 bool is_signed = false; 9007 underlying_type.IsIntegerType(is_signed); 9008 9009 llvm::APSInt value(enum_value_bit_size, is_signed); 9010 value = enum_value; 9011 9012 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value); 9013} 9014 9015CompilerType 9016ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) { 9017 clang::QualType enum_qual_type(GetCanonicalQualType(type)); 9018 const clang::Type *clang_type = enum_qual_type.getTypePtr(); 9019 if (clang_type) { 9020 const clang::EnumType *enutype = 9021 llvm::dyn_cast<clang::EnumType>(clang_type); 9022 if (enutype) { 9023 clang::EnumDecl *enum_decl = enutype->getDecl(); 9024 if (enum_decl) 9025 return CompilerType(getASTContext(), enum_decl->getIntegerType()); 9026 } 9027 } 9028 return CompilerType(); 9029} 9030 9031CompilerType 9032ClangASTContext::CreateMemberPointerType(const CompilerType &type, 9033 const CompilerType &pointee_type) { 9034 if (type && pointee_type.IsValid() && 9035 type.GetTypeSystem() == pointee_type.GetTypeSystem()) { 9036 ClangASTContext *ast = 9037 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); 9038 if (!ast) 9039 return CompilerType(); 9040 return CompilerType(ast->getASTContext(), 9041 ast->getASTContext()->getMemberPointerType( 9042 ClangUtil::GetQualType(pointee_type), 9043 ClangUtil::GetQualType(type).getTypePtr())); 9044 } 9045 return CompilerType(); 9046} 9047 9048size_t 9049ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type, 9050 const char *s, uint8_t *dst, 9051 size_t dst_size) { 9052 if (type) { 9053 clang::QualType qual_type(GetCanonicalQualType(type)); 9054 uint32_t count = 0; 9055 bool is_complex = false; 9056 if (IsFloatingPointType(type, count, is_complex)) { 9057 // TODO: handle complex and vector types 9058 if (count != 1) 9059 return false; 9060 9061 llvm::StringRef s_sref(s); 9062 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), 9063 s_sref); 9064 9065 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type); 9066 const uint64_t byte_size = bit_size / 8; 9067 if (dst_size >= byte_size) { 9068 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc( 9069 llvm::NextPowerOf2(byte_size) * 8); 9070 lldb_private::Status get_data_error; 9071 if (scalar.GetAsMemoryData(dst, byte_size, 9072 lldb_private::endian::InlHostByteOrder(), 9073 get_data_error)) 9074 return byte_size; 9075 } 9076 } 9077 } 9078 return 0; 9079} 9080 9081//---------------------------------------------------------------------- 9082// Dumping types 9083//---------------------------------------------------------------------- 9084#define DEPTH_INCREMENT 2 9085 9086void ClangASTContext::Dump(Stream &s) { 9087 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl()); 9088 tu->dump(s.AsRawOstream()); 9089} 9090 9091void ClangASTContext::DumpValue( 9092 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, 9093 lldb::Format format, const DataExtractor &data, 9094 lldb::offset_t data_byte_offset, size_t data_byte_size, 9095 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types, 9096 bool show_summary, bool verbose, uint32_t depth) { 9097 if (!type) 9098 return; 9099 9100 clang::QualType qual_type(GetQualType(type)); 9101 switch (qual_type->getTypeClass()) { 9102 case clang::Type::Record: 9103 if (GetCompleteType(type)) { 9104 const clang::RecordType *record_type = 9105 llvm::cast<clang::RecordType>(qual_type.getTypePtr()); 9106 const clang::RecordDecl *record_decl = record_type->getDecl(); 9107 assert(record_decl); 9108 uint32_t field_bit_offset = 0; 9109 uint32_t field_byte_offset = 0; 9110 const clang::ASTRecordLayout &record_layout = 9111 getASTContext()->getASTRecordLayout(record_decl); 9112 uint32_t child_idx = 0; 9113 9114 const clang::CXXRecordDecl *cxx_record_decl = 9115 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); 9116 if (cxx_record_decl) { 9117 // We might have base classes to print out first 9118 clang::CXXRecordDecl::base_class_const_iterator base_class, 9119 base_class_end; 9120 for (base_class = cxx_record_decl->bases_begin(), 9121 base_class_end = cxx_record_decl->bases_end(); 9122 base_class != base_class_end; ++base_class) { 9123 const clang::CXXRecordDecl *base_class_decl = 9124 llvm::cast<clang::CXXRecordDecl>( 9125 base_class->getType()->getAs<clang::RecordType>()->getDecl()); 9126 9127 // Skip empty base classes 9128 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl)) 9129 continue; 9130 9131 if (base_class->isVirtual()) 9132 field_bit_offset = 9133 record_layout.getVBaseClassOffset(base_class_decl) 9134 .getQuantity() * 9135 8; 9136 else 9137 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl) 9138 .getQuantity() * 9139 8; 9140 field_byte_offset = field_bit_offset / 8; 9141 assert(field_bit_offset % 8 == 0); 9142 if (child_idx == 0) 9143 s->PutChar('{'); 9144 else 9145 s->PutChar(','); 9146 9147 clang::QualType base_class_qual_type = base_class->getType(); 9148 std::string base_class_type_name(base_class_qual_type.getAsString()); 9149 9150 // Indent and print the base class type name 9151 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT), 9152 base_class_type_name); 9153 9154 clang::TypeInfo base_class_type_info = 9155 getASTContext()->getTypeInfo(base_class_qual_type); 9156 9157 // Dump the value of the member 9158 CompilerType base_clang_type(getASTContext(), base_class_qual_type); 9159 base_clang_type.DumpValue( 9160 exe_ctx, 9161 s, // Stream to dump to 9162 base_clang_type 9163 .GetFormat(), // The format with which to display the member 9164 data, // Data buffer containing all bytes for this type 9165 data_byte_offset + field_byte_offset, // Offset into "data" where 9166 // to grab value from 9167 base_class_type_info.Width / 8, // Size of this type in bytes 9168 0, // Bitfield bit size 9169 0, // Bitfield bit offset 9170 show_types, // Boolean indicating if we should show the variable 9171 // types 9172 show_summary, // Boolean indicating if we should show a summary 9173 // for the current type 9174 verbose, // Verbose output? 9175 depth + DEPTH_INCREMENT); // Scope depth for any types that have 9176 // children 9177 9178 ++child_idx; 9179 } 9180 } 9181 uint32_t field_idx = 0; 9182 clang::RecordDecl::field_iterator field, field_end; 9183 for (field = record_decl->field_begin(), 9184 field_end = record_decl->field_end(); 9185 field != field_end; ++field, ++field_idx, ++child_idx) { 9186 // Print the starting squiggly bracket (if this is the first member) or 9187 // comma (for member 2 and beyond) for the struct/union/class member. 9188 if (child_idx == 0) 9189 s->PutChar('{'); 9190 else 9191 s->PutChar(','); 9192 9193 // Indent 9194 s->Printf("\n%*s", depth + DEPTH_INCREMENT, ""); 9195 9196 clang::QualType field_type = field->getType(); 9197 // Print the member type if requested 9198 // Figure out the type byte size (field_type_info.first) and alignment 9199 // (field_type_info.second) from the AST context. 9200 clang::TypeInfo field_type_info = 9201 getASTContext()->getTypeInfo(field_type); 9202 assert(field_idx < record_layout.getFieldCount()); 9203 // Figure out the field offset within the current struct/union/class 9204 // type 9205 field_bit_offset = record_layout.getFieldOffset(field_idx); 9206 field_byte_offset = field_bit_offset / 8; 9207 uint32_t field_bitfield_bit_size = 0; 9208 uint32_t field_bitfield_bit_offset = 0; 9209 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field, 9210 field_bitfield_bit_size)) 9211 field_bitfield_bit_offset = field_bit_offset % 8; 9212 9213 if (show_types) { 9214 std::string field_type_name(field_type.getAsString()); 9215 if (field_bitfield_bit_size > 0) 9216 s->Printf("(%s:%u) ", field_type_name.c_str(), 9217 field_bitfield_bit_size); 9218 else 9219 s->Printf("(%s) ", field_type_name.c_str()); 9220 } 9221 // Print the member name and equal sign 9222 s->Printf("%s = ", field->getNameAsString().c_str()); 9223 9224 // Dump the value of the member 9225 CompilerType field_clang_type(getASTContext(), field_type); 9226 field_clang_type.DumpValue( 9227 exe_ctx, 9228 s, // Stream to dump to 9229 field_clang_type 9230 .GetFormat(), // The format with which to display the member 9231 data, // Data buffer containing all bytes for this type 9232 data_byte_offset + field_byte_offset, // Offset into "data" where to 9233 // grab value from 9234 field_type_info.Width / 8, // Size of this type in bytes 9235 field_bitfield_bit_size, // Bitfield bit size 9236 field_bitfield_bit_offset, // Bitfield bit offset 9237 show_types, // Boolean indicating if we should show the variable 9238 // types 9239 show_summary, // Boolean indicating if we should show a summary for 9240 // the current type 9241 verbose, // Verbose output? 9242 depth + DEPTH_INCREMENT); // Scope depth for any types that have 9243 // children 9244 } 9245 9246 // Indent the trailing squiggly bracket 9247 if (child_idx > 0) 9248 s->Printf("\n%*s}", depth, ""); 9249 } 9250 return; 9251 9252 case clang::Type::Enum: 9253 if (GetCompleteType(type)) { 9254 const clang::EnumType *enutype = 9255 llvm::cast<clang::EnumType>(qual_type.getTypePtr()); 9256 const clang::EnumDecl *enum_decl = enutype->getDecl(); 9257 assert(enum_decl); 9258 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos; 9259 lldb::offset_t offset = data_byte_offset; 9260 const int64_t enum_value = data.GetMaxU64Bitfield( 9261 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset); 9262 for (enum_pos = enum_decl->enumerator_begin(), 9263 enum_end_pos = enum_decl->enumerator_end(); 9264 enum_pos != enum_end_pos; ++enum_pos) { 9265 if (enum_pos->getInitVal() == enum_value) { 9266 s->Printf("%s", enum_pos->getNameAsString().c_str()); 9267 return; 9268 } 9269 } 9270 // If we have gotten here we didn't get find the enumerator in the enum 9271 // decl, so just print the integer. 9272 s->Printf("%" PRIi64, enum_value); 9273 } 9274 return; 9275 9276 case clang::Type::ConstantArray: { 9277 const clang::ConstantArrayType *array = 9278 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr()); 9279 bool is_array_of_characters = false; 9280 clang::QualType element_qual_type = array->getElementType(); 9281 9282 const clang::Type *canonical_type = 9283 element_qual_type->getCanonicalTypeInternal().getTypePtr(); 9284 if (canonical_type) 9285 is_array_of_characters = canonical_type->isCharType(); 9286 9287 const uint64_t element_count = array->getSize().getLimitedValue(); 9288 9289 clang::TypeInfo field_type_info = 9290 getASTContext()->getTypeInfo(element_qual_type); 9291 9292 uint32_t element_idx = 0; 9293 uint32_t element_offset = 0; 9294 uint64_t element_byte_size = field_type_info.Width / 8; 9295 uint32_t element_stride = element_byte_size; 9296 9297 if (is_array_of_characters) { 9298 s->PutChar('"'); 9299 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar, 9300 element_byte_size, element_count, UINT32_MAX, 9301 LLDB_INVALID_ADDRESS, 0, 0); 9302 s->PutChar('"'); 9303 return; 9304 } else { 9305 CompilerType element_clang_type(getASTContext(), element_qual_type); 9306 lldb::Format element_format = element_clang_type.GetFormat(); 9307 9308 for (element_idx = 0; element_idx < element_count; ++element_idx) { 9309 // Print the starting squiggly bracket (if this is the first member) or 9310 // comman (for member 2 and beyong) for the struct/union/class member. 9311 if (element_idx == 0) 9312 s->PutChar('{'); 9313 else 9314 s->PutChar(','); 9315 9316 // Indent and print the index 9317 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx); 9318 9319 // Figure out the field offset within the current struct/union/class 9320 // type 9321 element_offset = element_idx * element_stride; 9322 9323 // Dump the value of the member 9324 element_clang_type.DumpValue( 9325 exe_ctx, 9326 s, // Stream to dump to 9327 element_format, // The format with which to display the element 9328 data, // Data buffer containing all bytes for this type 9329 data_byte_offset + 9330 element_offset, // Offset into "data" where to grab value from 9331 element_byte_size, // Size of this type in bytes 9332 0, // Bitfield bit size 9333 0, // Bitfield bit offset 9334 show_types, // Boolean indicating if we should show the variable 9335 // types 9336 show_summary, // Boolean indicating if we should show a summary for 9337 // the current type 9338 verbose, // Verbose output? 9339 depth + DEPTH_INCREMENT); // Scope depth for any types that have 9340 // children 9341 } 9342 9343 // Indent the trailing squiggly bracket 9344 if (element_idx > 0) 9345 s->Printf("\n%*s}", depth, ""); 9346 } 9347 } 9348 return; 9349 9350 case clang::Type::Typedef: { 9351 clang::QualType typedef_qual_type = 9352 llvm::cast<clang::TypedefType>(qual_type) 9353 ->getDecl() 9354 ->getUnderlyingType(); 9355 9356 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type); 9357 lldb::Format typedef_format = typedef_clang_type.GetFormat(); 9358 clang::TypeInfo typedef_type_info = 9359 getASTContext()->getTypeInfo(typedef_qual_type); 9360 uint64_t typedef_byte_size = typedef_type_info.Width / 8; 9361 9362 return typedef_clang_type.DumpValue( 9363 exe_ctx, 9364 s, // Stream to dump to 9365 typedef_format, // The format with which to display the element 9366 data, // Data buffer containing all bytes for this type 9367 data_byte_offset, // Offset into "data" where to grab value from 9368 typedef_byte_size, // Size of this type in bytes 9369 bitfield_bit_size, // Bitfield bit size 9370 bitfield_bit_offset, // Bitfield bit offset 9371 show_types, // Boolean indicating if we should show the variable types 9372 show_summary, // Boolean indicating if we should show a summary for the 9373 // current type 9374 verbose, // Verbose output? 9375 depth); // Scope depth for any types that have children 9376 } break; 9377 9378 case clang::Type::Auto: { 9379 clang::QualType elaborated_qual_type = 9380 llvm::cast<clang::AutoType>(qual_type)->getDeducedType(); 9381 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type); 9382 lldb::Format elaborated_format = elaborated_clang_type.GetFormat(); 9383 clang::TypeInfo elaborated_type_info = 9384 getASTContext()->getTypeInfo(elaborated_qual_type); 9385 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8; 9386 9387 return elaborated_clang_type.DumpValue( 9388 exe_ctx, 9389 s, // Stream to dump to 9390 elaborated_format, // The format with which to display the element 9391 data, // Data buffer containing all bytes for this type 9392 data_byte_offset, // Offset into "data" where to grab value from 9393 elaborated_byte_size, // Size of this type in bytes 9394 bitfield_bit_size, // Bitfield bit size 9395 bitfield_bit_offset, // Bitfield bit offset 9396 show_types, // Boolean indicating if we should show the variable types 9397 show_summary, // Boolean indicating if we should show a summary for the 9398 // current type 9399 verbose, // Verbose output? 9400 depth); // Scope depth for any types that have children 9401 } break; 9402 9403 case clang::Type::Elaborated: { 9404 clang::QualType elaborated_qual_type = 9405 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(); 9406 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type); 9407 lldb::Format elaborated_format = elaborated_clang_type.GetFormat(); 9408 clang::TypeInfo elaborated_type_info = 9409 getASTContext()->getTypeInfo(elaborated_qual_type); 9410 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8; 9411 9412 return elaborated_clang_type.DumpValue( 9413 exe_ctx, 9414 s, // Stream to dump to 9415 elaborated_format, // The format with which to display the element 9416 data, // Data buffer containing all bytes for this type 9417 data_byte_offset, // Offset into "data" where to grab value from 9418 elaborated_byte_size, // Size of this type in bytes 9419 bitfield_bit_size, // Bitfield bit size 9420 bitfield_bit_offset, // Bitfield bit offset 9421 show_types, // Boolean indicating if we should show the variable types 9422 show_summary, // Boolean indicating if we should show a summary for the 9423 // current type 9424 verbose, // Verbose output? 9425 depth); // Scope depth for any types that have children 9426 } break; 9427 9428 case clang::Type::Paren: { 9429 clang::QualType desugar_qual_type = 9430 llvm::cast<clang::ParenType>(qual_type)->desugar(); 9431 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type); 9432 9433 lldb::Format desugar_format = desugar_clang_type.GetFormat(); 9434 clang::TypeInfo desugar_type_info = 9435 getASTContext()->getTypeInfo(desugar_qual_type); 9436 uint64_t desugar_byte_size = desugar_type_info.Width / 8; 9437 9438 return desugar_clang_type.DumpValue( 9439 exe_ctx, 9440 s, // Stream to dump to 9441 desugar_format, // The format with which to display the element 9442 data, // Data buffer containing all bytes for this type 9443 data_byte_offset, // Offset into "data" where to grab value from 9444 desugar_byte_size, // Size of this type in bytes 9445 bitfield_bit_size, // Bitfield bit size 9446 bitfield_bit_offset, // Bitfield bit offset 9447 show_types, // Boolean indicating if we should show the variable types 9448 show_summary, // Boolean indicating if we should show a summary for the 9449 // current type 9450 verbose, // Verbose output? 9451 depth); // Scope depth for any types that have children 9452 } break; 9453 9454 default: 9455 // We are down to a scalar type that we just need to display. 9456 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1, 9457 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size, 9458 bitfield_bit_offset); 9459 9460 if (show_summary) 9461 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size); 9462 break; 9463 } 9464} 9465 9466bool ClangASTContext::DumpTypeValue( 9467 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format, 9468 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size, 9469 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 9470 ExecutionContextScope *exe_scope) { 9471 if (!type) 9472 return false; 9473 if (IsAggregateType(type)) { 9474 return false; 9475 } else { 9476 clang::QualType qual_type(GetQualType(type)); 9477 9478 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 9479 switch (type_class) { 9480 case clang::Type::Typedef: { 9481 clang::QualType typedef_qual_type = 9482 llvm::cast<clang::TypedefType>(qual_type) 9483 ->getDecl() 9484 ->getUnderlyingType(); 9485 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type); 9486 if (format == eFormatDefault) 9487 format = typedef_clang_type.GetFormat(); 9488 clang::TypeInfo typedef_type_info = 9489 getASTContext()->getTypeInfo(typedef_qual_type); 9490 uint64_t typedef_byte_size = typedef_type_info.Width / 8; 9491 9492 return typedef_clang_type.DumpTypeValue( 9493 s, 9494 format, // The format with which to display the element 9495 data, // Data buffer containing all bytes for this type 9496 byte_offset, // Offset into "data" where to grab value from 9497 typedef_byte_size, // Size of this type in bytes 9498 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't 9499 // treat as a bitfield 9500 bitfield_bit_offset, // Offset in bits of a bitfield value if 9501 // bitfield_bit_size != 0 9502 exe_scope); 9503 } break; 9504 9505 case clang::Type::Enum: 9506 // If our format is enum or default, show the enumeration value as its 9507 // enumeration string value, else just display it as requested. 9508 if ((format == eFormatEnum || format == eFormatDefault) && 9509 GetCompleteType(type)) { 9510 const clang::EnumType *enutype = 9511 llvm::cast<clang::EnumType>(qual_type.getTypePtr()); 9512 const clang::EnumDecl *enum_decl = enutype->getDecl(); 9513 assert(enum_decl); 9514 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos; 9515 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType(); 9516 lldb::offset_t offset = byte_offset; 9517 if (is_signed) { 9518 const int64_t enum_svalue = data.GetMaxS64Bitfield( 9519 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset); 9520 for (enum_pos = enum_decl->enumerator_begin(), 9521 enum_end_pos = enum_decl->enumerator_end(); 9522 enum_pos != enum_end_pos; ++enum_pos) { 9523 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) { 9524 s->PutCString(enum_pos->getNameAsString()); 9525 return true; 9526 } 9527 } 9528 // If we have gotten here we didn't get find the enumerator in the 9529 // enum decl, so just print the integer. 9530 s->Printf("%" PRIi64, enum_svalue); 9531 } else { 9532 const uint64_t enum_uvalue = data.GetMaxU64Bitfield( 9533 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset); 9534 for (enum_pos = enum_decl->enumerator_begin(), 9535 enum_end_pos = enum_decl->enumerator_end(); 9536 enum_pos != enum_end_pos; ++enum_pos) { 9537 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) { 9538 s->PutCString(enum_pos->getNameAsString()); 9539 return true; 9540 } 9541 } 9542 // If we have gotten here we didn't get find the enumerator in the 9543 // enum decl, so just print the integer. 9544 s->Printf("%" PRIu64, enum_uvalue); 9545 } 9546 return true; 9547 } 9548 // format was not enum, just fall through and dump the value as 9549 // requested.... 9550 LLVM_FALLTHROUGH; 9551 9552 default: 9553 // We are down to a scalar type that we just need to display. 9554 { 9555 uint32_t item_count = 1; 9556 // A few formats, we might need to modify our size and count for 9557 // depending 9558 // on how we are trying to display the value... 9559 switch (format) { 9560 default: 9561 case eFormatBoolean: 9562 case eFormatBinary: 9563 case eFormatComplex: 9564 case eFormatCString: // NULL terminated C strings 9565 case eFormatDecimal: 9566 case eFormatEnum: 9567 case eFormatHex: 9568 case eFormatHexUppercase: 9569 case eFormatFloat: 9570 case eFormatOctal: 9571 case eFormatOSType: 9572 case eFormatUnsigned: 9573 case eFormatPointer: 9574 case eFormatVectorOfChar: 9575 case eFormatVectorOfSInt8: 9576 case eFormatVectorOfUInt8: 9577 case eFormatVectorOfSInt16: 9578 case eFormatVectorOfUInt16: 9579 case eFormatVectorOfSInt32: 9580 case eFormatVectorOfUInt32: 9581 case eFormatVectorOfSInt64: 9582 case eFormatVectorOfUInt64: 9583 case eFormatVectorOfFloat32: 9584 case eFormatVectorOfFloat64: 9585 case eFormatVectorOfUInt128: 9586 break; 9587 9588 case eFormatChar: 9589 case eFormatCharPrintable: 9590 case eFormatCharArray: 9591 case eFormatBytes: 9592 case eFormatBytesWithASCII: 9593 item_count = byte_size; 9594 byte_size = 1; 9595 break; 9596 9597 case eFormatUnicode16: 9598 item_count = byte_size / 2; 9599 byte_size = 2; 9600 break; 9601 9602 case eFormatUnicode32: 9603 item_count = byte_size / 4; 9604 byte_size = 4; 9605 break; 9606 } 9607 return DumpDataExtractor(data, s, byte_offset, format, byte_size, 9608 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 9609 bitfield_bit_size, bitfield_bit_offset, 9610 exe_scope); 9611 } 9612 break; 9613 } 9614 } 9615 return 0; 9616} 9617 9618void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type, 9619 ExecutionContext *exe_ctx, Stream *s, 9620 const lldb_private::DataExtractor &data, 9621 lldb::offset_t data_byte_offset, 9622 size_t data_byte_size) { 9623 uint32_t length = 0; 9624 if (IsCStringType(type, length)) { 9625 if (exe_ctx) { 9626 Process *process = exe_ctx->GetProcessPtr(); 9627 if (process) { 9628 lldb::offset_t offset = data_byte_offset; 9629 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size); 9630 std::vector<uint8_t> buf; 9631 if (length > 0) 9632 buf.resize(length); 9633 else 9634 buf.resize(256); 9635 9636 DataExtractor cstr_data(&buf.front(), buf.size(), 9637 process->GetByteOrder(), 4); 9638 buf.back() = '\0'; 9639 size_t bytes_read; 9640 size_t total_cstr_len = 0; 9641 Status error; 9642 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(), 9643 buf.size(), error)) > 0) { 9644 const size_t len = strlen((const char *)&buf.front()); 9645 if (len == 0) 9646 break; 9647 if (total_cstr_len == 0) 9648 s->PutCString(" \""); 9649 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len, 9650 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0); 9651 total_cstr_len += len; 9652 if (len < buf.size()) 9653 break; 9654 pointer_address += total_cstr_len; 9655 } 9656 if (total_cstr_len > 0) 9657 s->PutChar('"'); 9658 } 9659 } 9660 } 9661} 9662 9663void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) { 9664 StreamFile s(stdout, false); 9665 DumpTypeDescription(type, &s); 9666 ClangASTMetadata *metadata = 9667 ClangASTContext::GetMetadata(getASTContext(), type); 9668 if (metadata) { 9669 metadata->Dump(&s); 9670 } 9671} 9672 9673void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type, 9674 Stream *s) { 9675 if (type) { 9676 clang::QualType qual_type(GetQualType(type)); 9677 9678 llvm::SmallVector<char, 1024> buf; 9679 llvm::raw_svector_ostream llvm_ostrm(buf); 9680 9681 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 9682 switch (type_class) { 9683 case clang::Type::ObjCObject: 9684 case clang::Type::ObjCInterface: { 9685 GetCompleteType(type); 9686 9687 const clang::ObjCObjectType *objc_class_type = 9688 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); 9689 assert(objc_class_type); 9690 if (objc_class_type) { 9691 clang::ObjCInterfaceDecl *class_interface_decl = 9692 objc_class_type->getInterface(); 9693 if (class_interface_decl) { 9694 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy(); 9695 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel()); 9696 } 9697 } 9698 } break; 9699 9700 case clang::Type::Typedef: { 9701 const clang::TypedefType *typedef_type = 9702 qual_type->getAs<clang::TypedefType>(); 9703 if (typedef_type) { 9704 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl(); 9705 std::string clang_typedef_name( 9706 typedef_decl->getQualifiedNameAsString()); 9707 if (!clang_typedef_name.empty()) { 9708 s->PutCString("typedef "); 9709 s->PutCString(clang_typedef_name); 9710 } 9711 } 9712 } break; 9713 9714 case clang::Type::Auto: 9715 CompilerType(getASTContext(), 9716 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) 9717 .DumpTypeDescription(s); 9718 return; 9719 9720 case clang::Type::Elaborated: 9721 CompilerType(getASTContext(), 9722 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) 9723 .DumpTypeDescription(s); 9724 return; 9725 9726 case clang::Type::Paren: 9727 CompilerType(getASTContext(), 9728 llvm::cast<clang::ParenType>(qual_type)->desugar()) 9729 .DumpTypeDescription(s); 9730 return; 9731 9732 case clang::Type::Record: { 9733 GetCompleteType(type); 9734 9735 const clang::RecordType *record_type = 9736 llvm::cast<clang::RecordType>(qual_type.getTypePtr()); 9737 const clang::RecordDecl *record_decl = record_type->getDecl(); 9738 const clang::CXXRecordDecl *cxx_record_decl = 9739 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); 9740 9741 if (cxx_record_decl) 9742 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), 9743 s->GetIndentLevel()); 9744 else 9745 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), 9746 s->GetIndentLevel()); 9747 } break; 9748 9749 default: { 9750 const clang::TagType *tag_type = 9751 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()); 9752 if (tag_type) { 9753 clang::TagDecl *tag_decl = tag_type->getDecl(); 9754 if (tag_decl) 9755 tag_decl->print(llvm_ostrm, 0); 9756 } else { 9757 std::string clang_type_name(qual_type.getAsString()); 9758 if (!clang_type_name.empty()) 9759 s->PutCString(clang_type_name); 9760 } 9761 } 9762 } 9763 9764 if (buf.size() > 0) { 9765 s->Write(buf.data(), buf.size()); 9766 } 9767 } 9768} 9769 9770void ClangASTContext::DumpTypeName(const CompilerType &type) { 9771 if (ClangUtil::IsClangType(type)) { 9772 clang::QualType qual_type( 9773 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type))); 9774 9775 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 9776 switch (type_class) { 9777 case clang::Type::Record: { 9778 const clang::CXXRecordDecl *cxx_record_decl = 9779 qual_type->getAsCXXRecordDecl(); 9780 if (cxx_record_decl) 9781 printf("class %s", cxx_record_decl->getName().str().c_str()); 9782 } break; 9783 9784 case clang::Type::Enum: { 9785 clang::EnumDecl *enum_decl = 9786 llvm::cast<clang::EnumType>(qual_type)->getDecl(); 9787 if (enum_decl) { 9788 printf("enum %s", enum_decl->getName().str().c_str()); 9789 } 9790 } break; 9791 9792 case clang::Type::ObjCObject: 9793 case clang::Type::ObjCInterface: { 9794 const clang::ObjCObjectType *objc_class_type = 9795 llvm::dyn_cast<clang::ObjCObjectType>(qual_type); 9796 if (objc_class_type) { 9797 clang::ObjCInterfaceDecl *class_interface_decl = 9798 objc_class_type->getInterface(); 9799 // We currently can't complete objective C types through the newly 9800 // added ASTContext because it only supports TagDecl objects right 9801 // now... 9802 if (class_interface_decl) 9803 printf("@class %s", class_interface_decl->getName().str().c_str()); 9804 } 9805 } break; 9806 9807 case clang::Type::Typedef: 9808 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type) 9809 ->getDecl() 9810 ->getName() 9811 .str() 9812 .c_str()); 9813 break; 9814 9815 case clang::Type::Auto: 9816 printf("auto "); 9817 return DumpTypeName(CompilerType(type.GetTypeSystem(), 9818 llvm::cast<clang::AutoType>(qual_type) 9819 ->getDeducedType() 9820 .getAsOpaquePtr())); 9821 9822 case clang::Type::Elaborated: 9823 printf("elaborated "); 9824 return DumpTypeName(CompilerType( 9825 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type) 9826 ->getNamedType() 9827 .getAsOpaquePtr())); 9828 9829 case clang::Type::Paren: 9830 printf("paren "); 9831 return DumpTypeName(CompilerType( 9832 type.GetTypeSystem(), 9833 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())); 9834 9835 default: 9836 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class); 9837 break; 9838 } 9839 } 9840} 9841 9842clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl( 9843 clang::DeclContext *decl_ctx, lldb::AccessType access_type, 9844 const char *parent_name, int tag_decl_kind, 9845 const ClangASTContext::TemplateParameterInfos &template_param_infos) { 9846 if (template_param_infos.IsValid()) { 9847 std::string template_basename(parent_name); 9848 template_basename.erase(template_basename.find('<')); 9849 9850 return CreateClassTemplateDecl(decl_ctx, access_type, 9851 template_basename.c_str(), tag_decl_kind, 9852 template_param_infos); 9853 } 9854 return NULL; 9855} 9856 9857void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) { 9858 ClangASTContext *ast = (ClangASTContext *)baton; 9859 SymbolFile *sym_file = ast->GetSymbolFile(); 9860 if (sym_file) { 9861 CompilerType clang_type = GetTypeForDecl(decl); 9862 if (clang_type) 9863 sym_file->CompleteType(clang_type); 9864 } 9865} 9866 9867void ClangASTContext::CompleteObjCInterfaceDecl( 9868 void *baton, clang::ObjCInterfaceDecl *decl) { 9869 ClangASTContext *ast = (ClangASTContext *)baton; 9870 SymbolFile *sym_file = ast->GetSymbolFile(); 9871 if (sym_file) { 9872 CompilerType clang_type = GetTypeForDecl(decl); 9873 if (clang_type) 9874 sym_file->CompleteType(clang_type); 9875 } 9876} 9877 9878DWARFASTParser *ClangASTContext::GetDWARFParser() { 9879 if (!m_dwarf_ast_parser_ap) 9880 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this)); 9881 return m_dwarf_ast_parser_ap.get(); 9882} 9883 9884#ifdef LLDB_ENABLE_ALL 9885PDBASTParser *ClangASTContext::GetPDBParser() { 9886 if (!m_pdb_ast_parser_ap) 9887 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this)); 9888 return m_pdb_ast_parser_ap.get(); 9889} 9890#endif // LLDB_ENABLE_ALL 9891 9892bool ClangASTContext::LayoutRecordType( 9893 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size, 9894 uint64_t &alignment, 9895 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets, 9896 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> 9897 &base_offsets, 9898 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> 9899 &vbase_offsets) { 9900 ClangASTContext *ast = (ClangASTContext *)baton; 9901 lldb_private::ClangASTImporter *importer = nullptr; 9902 if (ast->m_dwarf_ast_parser_ap) 9903 importer = &ast->m_dwarf_ast_parser_ap->GetClangASTImporter(); 9904#ifdef LLDB_ENABLE_ALL 9905 if (!importer && ast->m_pdb_ast_parser_ap) 9906 importer = &ast->m_pdb_ast_parser_ap->GetClangASTImporter(); 9907#endif // LLDB_ENABLE_ALL 9908 if (!importer) 9909 return false; 9910 9911 return importer->LayoutRecordType(record_decl, bit_size, alignment, 9912 field_offsets, base_offsets, vbase_offsets); 9913} 9914 9915//---------------------------------------------------------------------- 9916// CompilerDecl override functions 9917//---------------------------------------------------------------------- 9918 9919ConstString ClangASTContext::DeclGetName(void *opaque_decl) { 9920 if (opaque_decl) { 9921 clang::NamedDecl *nd = 9922 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl); 9923 if (nd != nullptr) 9924 return ConstString(nd->getDeclName().getAsString()); 9925 } 9926 return ConstString(); 9927} 9928 9929ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) { 9930 if (opaque_decl) { 9931 clang::NamedDecl *nd = 9932 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl); 9933 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) { 9934 clang::MangleContext *mc = getMangleContext(); 9935 if (mc && mc->shouldMangleCXXName(nd)) { 9936 llvm::SmallVector<char, 1024> buf; 9937 llvm::raw_svector_ostream llvm_ostrm(buf); 9938 if (llvm::isa<clang::CXXConstructorDecl>(nd)) { 9939 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd), 9940 Ctor_Complete, llvm_ostrm); 9941 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) { 9942 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd), 9943 Dtor_Complete, llvm_ostrm); 9944 } else { 9945 mc->mangleName(nd, llvm_ostrm); 9946 } 9947 if (buf.size() > 0) 9948 return ConstString(buf.data(), buf.size()); 9949 } 9950 } 9951 } 9952 return ConstString(); 9953} 9954 9955CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) { 9956 if (opaque_decl) 9957 return CompilerDeclContext(this, 9958 ((clang::Decl *)opaque_decl)->getDeclContext()); 9959 else 9960 return CompilerDeclContext(); 9961} 9962 9963CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) { 9964 if (clang::FunctionDecl *func_decl = 9965 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) 9966 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr()); 9967 if (clang::ObjCMethodDecl *objc_method = 9968 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl)) 9969 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr()); 9970 else 9971 return CompilerType(); 9972} 9973 9974size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) { 9975 if (clang::FunctionDecl *func_decl = 9976 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) 9977 return func_decl->param_size(); 9978 if (clang::ObjCMethodDecl *objc_method = 9979 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl)) 9980 return objc_method->param_size(); 9981 else 9982 return 0; 9983} 9984 9985CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl, 9986 size_t idx) { 9987 if (clang::FunctionDecl *func_decl = 9988 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) { 9989 if (idx < func_decl->param_size()) { 9990 ParmVarDecl *var_decl = func_decl->getParamDecl(idx); 9991 if (var_decl) 9992 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr()); 9993 } 9994 } else if (clang::ObjCMethodDecl *objc_method = 9995 llvm::dyn_cast<clang::ObjCMethodDecl>( 9996 (clang::Decl *)opaque_decl)) { 9997 if (idx < objc_method->param_size()) 9998 return CompilerType( 9999 this, 10000 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr()); 10001 } 10002 return CompilerType(); 10003} 10004 10005//---------------------------------------------------------------------- 10006// CompilerDeclContext functions 10007//---------------------------------------------------------------------- 10008 10009std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName( 10010 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) { 10011 std::vector<CompilerDecl> found_decls; 10012 if (opaque_decl_ctx) { 10013 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx; 10014 std::set<DeclContext *> searched; 10015 std::multimap<DeclContext *, DeclContext *> search_queue; 10016 SymbolFile *symbol_file = GetSymbolFile(); 10017 10018 for (clang::DeclContext *decl_context = root_decl_ctx; 10019 decl_context != nullptr && found_decls.empty(); 10020 decl_context = decl_context->getParent()) { 10021 search_queue.insert(std::make_pair(decl_context, decl_context)); 10022 10023 for (auto it = search_queue.find(decl_context); it != search_queue.end(); 10024 it++) { 10025 if (!searched.insert(it->second).second) 10026 continue; 10027 symbol_file->ParseDeclsForContext( 10028 CompilerDeclContext(this, it->second)); 10029 10030 for (clang::Decl *child : it->second->decls()) { 10031 if (clang::UsingDirectiveDecl *ud = 10032 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) { 10033 if (ignore_using_decls) 10034 continue; 10035 clang::DeclContext *from = ud->getCommonAncestor(); 10036 if (searched.find(ud->getNominatedNamespace()) == searched.end()) 10037 search_queue.insert( 10038 std::make_pair(from, ud->getNominatedNamespace())); 10039 } else if (clang::UsingDecl *ud = 10040 llvm::dyn_cast<clang::UsingDecl>(child)) { 10041 if (ignore_using_decls) 10042 continue; 10043 for (clang::UsingShadowDecl *usd : ud->shadows()) { 10044 clang::Decl *target = usd->getTargetDecl(); 10045 if (clang::NamedDecl *nd = 10046 llvm::dyn_cast<clang::NamedDecl>(target)) { 10047 IdentifierInfo *ii = nd->getIdentifier(); 10048 if (ii != nullptr && 10049 ii->getName().equals(name.AsCString(nullptr))) 10050 found_decls.push_back(CompilerDecl(this, nd)); 10051 } 10052 } 10053 } else if (clang::NamedDecl *nd = 10054 llvm::dyn_cast<clang::NamedDecl>(child)) { 10055 IdentifierInfo *ii = nd->getIdentifier(); 10056 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr))) 10057 found_decls.push_back(CompilerDecl(this, nd)); 10058 } 10059 } 10060 } 10061 } 10062 } 10063 return found_decls; 10064} 10065 10066// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents, 10067// and return the number of levels it took to find it, or 10068// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using 10069// declaration, its name and/or type, if set, will be used to check that the 10070// decl found in the scope is a match. 10071// 10072// The optional name is required by languages (like C++) to handle using 10073// declarations like: 10074// 10075// void poo(); 10076// namespace ns { 10077// void foo(); 10078// void goo(); 10079// } 10080// void bar() { 10081// using ns::foo; 10082// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and 10083// // LLDB_INVALID_DECL_LEVEL for 'goo'. 10084// } 10085// 10086// The optional type is useful in the case that there's a specific overload 10087// that we're looking for that might otherwise be shadowed, like: 10088// 10089// void foo(int); 10090// namespace ns { 10091// void foo(); 10092// } 10093// void bar() { 10094// using ns::foo; 10095// // CountDeclLevels returns 0 for { 'foo', void() }, 10096// // 1 for { 'foo', void(int) }, and 10097// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }. 10098// } 10099// 10100// NOTE: Because file statics are at the TranslationUnit along with globals, a 10101// function at file scope will return the same level as a function at global 10102// scope. Ideally we'd like to treat the file scope as an additional scope just 10103// below the global scope. More work needs to be done to recognise that, if 10104// the decl we're trying to look up is static, we should compare its source 10105// file with that of the current scope and return a lower number for it. 10106uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx, 10107 clang::DeclContext *child_decl_ctx, 10108 ConstString *child_name, 10109 CompilerType *child_type) { 10110 if (frame_decl_ctx) { 10111 std::set<DeclContext *> searched; 10112 std::multimap<DeclContext *, DeclContext *> search_queue; 10113 SymbolFile *symbol_file = GetSymbolFile(); 10114 10115 // Get the lookup scope for the decl we're trying to find. 10116 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent(); 10117 10118 // Look for it in our scope's decl context and its parents. 10119 uint32_t level = 0; 10120 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr; 10121 decl_ctx = decl_ctx->getParent()) { 10122 if (!decl_ctx->isLookupContext()) 10123 continue; 10124 if (decl_ctx == parent_decl_ctx) 10125 // Found it! 10126 return level; 10127 search_queue.insert(std::make_pair(decl_ctx, decl_ctx)); 10128 for (auto it = search_queue.find(decl_ctx); it != search_queue.end(); 10129 it++) { 10130 if (searched.find(it->second) != searched.end()) 10131 continue; 10132 10133 // Currently DWARF has one shared translation unit for all Decls at top 10134 // level, so this would erroneously find using statements anywhere. So 10135 // don't look at the top-level translation unit. 10136 // TODO fix this and add a testcase that depends on it. 10137 10138 if (llvm::isa<clang::TranslationUnitDecl>(it->second)) 10139 continue; 10140 10141 searched.insert(it->second); 10142 symbol_file->ParseDeclsForContext( 10143 CompilerDeclContext(this, it->second)); 10144 10145 for (clang::Decl *child : it->second->decls()) { 10146 if (clang::UsingDirectiveDecl *ud = 10147 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) { 10148 clang::DeclContext *ns = ud->getNominatedNamespace(); 10149 if (ns == parent_decl_ctx) 10150 // Found it! 10151 return level; 10152 clang::DeclContext *from = ud->getCommonAncestor(); 10153 if (searched.find(ns) == searched.end()) 10154 search_queue.insert(std::make_pair(from, ns)); 10155 } else if (child_name) { 10156 if (clang::UsingDecl *ud = 10157 llvm::dyn_cast<clang::UsingDecl>(child)) { 10158 for (clang::UsingShadowDecl *usd : ud->shadows()) { 10159 clang::Decl *target = usd->getTargetDecl(); 10160 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target); 10161 if (!nd) 10162 continue; 10163 // Check names. 10164 IdentifierInfo *ii = nd->getIdentifier(); 10165 if (ii == nullptr || 10166 !ii->getName().equals(child_name->AsCString(nullptr))) 10167 continue; 10168 // Check types, if one was provided. 10169 if (child_type) { 10170 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd); 10171 if (!AreTypesSame(clang_type, *child_type, 10172 /*ignore_qualifiers=*/true)) 10173 continue; 10174 } 10175 // Found it! 10176 return level; 10177 } 10178 } 10179 } 10180 } 10181 } 10182 ++level; 10183 } 10184 } 10185 return LLDB_INVALID_DECL_LEVEL; 10186} 10187 10188bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) { 10189 if (opaque_decl_ctx) 10190 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord(); 10191 else 10192 return false; 10193} 10194 10195ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) { 10196 if (opaque_decl_ctx) { 10197 clang::NamedDecl *named_decl = 10198 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx); 10199 if (named_decl) 10200 return ConstString(named_decl->getName()); 10201 } 10202 return ConstString(); 10203} 10204 10205ConstString 10206ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) { 10207 if (opaque_decl_ctx) { 10208 clang::NamedDecl *named_decl = 10209 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx); 10210 if (named_decl) 10211 return ConstString( 10212 llvm::StringRef(named_decl->getQualifiedNameAsString())); 10213 } 10214 return ConstString(); 10215} 10216 10217bool ClangASTContext::DeclContextIsClassMethod( 10218 void *opaque_decl_ctx, lldb::LanguageType *language_ptr, 10219 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) { 10220 if (opaque_decl_ctx) { 10221 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx; 10222 if (ObjCMethodDecl *objc_method = 10223 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) { 10224 if (is_instance_method_ptr) 10225 *is_instance_method_ptr = objc_method->isInstanceMethod(); 10226 if (language_ptr) 10227 *language_ptr = eLanguageTypeObjC; 10228 if (language_object_name_ptr) 10229 language_object_name_ptr->SetCString("self"); 10230 return true; 10231 } else if (CXXMethodDecl *cxx_method = 10232 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) { 10233 if (is_instance_method_ptr) 10234 *is_instance_method_ptr = cxx_method->isInstance(); 10235 if (language_ptr) 10236 *language_ptr = eLanguageTypeC_plus_plus; 10237 if (language_object_name_ptr) 10238 language_object_name_ptr->SetCString("this"); 10239 return true; 10240 } else if (clang::FunctionDecl *function_decl = 10241 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) { 10242 ClangASTMetadata *metadata = 10243 GetMetadata(&decl_ctx->getParentASTContext(), function_decl); 10244 if (metadata && metadata->HasObjectPtr()) { 10245 if (is_instance_method_ptr) 10246 *is_instance_method_ptr = true; 10247 if (language_ptr) 10248 *language_ptr = eLanguageTypeObjC; 10249 if (language_object_name_ptr) 10250 language_object_name_ptr->SetCString(metadata->GetObjectPtrName()); 10251 return true; 10252 } 10253 } 10254 } 10255 return false; 10256} 10257 10258clang::DeclContext * 10259ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) { 10260 if (dc.IsClang()) 10261 return (clang::DeclContext *)dc.GetOpaqueDeclContext(); 10262 return nullptr; 10263} 10264 10265ObjCMethodDecl * 10266ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) { 10267 if (dc.IsClang()) 10268 return llvm::dyn_cast<clang::ObjCMethodDecl>( 10269 (clang::DeclContext *)dc.GetOpaqueDeclContext()); 10270 return nullptr; 10271} 10272 10273CXXMethodDecl * 10274ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) { 10275 if (dc.IsClang()) 10276 return llvm::dyn_cast<clang::CXXMethodDecl>( 10277 (clang::DeclContext *)dc.GetOpaqueDeclContext()); 10278 return nullptr; 10279} 10280 10281clang::FunctionDecl * 10282ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) { 10283 if (dc.IsClang()) 10284 return llvm::dyn_cast<clang::FunctionDecl>( 10285 (clang::DeclContext *)dc.GetOpaqueDeclContext()); 10286 return nullptr; 10287} 10288 10289clang::NamespaceDecl * 10290ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) { 10291 if (dc.IsClang()) 10292 return llvm::dyn_cast<clang::NamespaceDecl>( 10293 (clang::DeclContext *)dc.GetOpaqueDeclContext()); 10294 return nullptr; 10295} 10296 10297ClangASTMetadata * 10298ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc, 10299 const void *object) { 10300 clang::ASTContext *ast = DeclContextGetClangASTContext(dc); 10301 if (ast) 10302 return ClangASTContext::GetMetadata(ast, object); 10303 return nullptr; 10304} 10305 10306clang::ASTContext * 10307ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) { 10308 ClangASTContext *ast = 10309 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem()); 10310 if (ast) 10311 return ast->getASTContext(); 10312 return nullptr; 10313} 10314 10315ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target) 10316 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()), 10317 m_target_wp(target.shared_from_this()), 10318 m_persistent_variables(new ClangPersistentVariables) {} 10319 10320UserExpression *ClangASTContextForExpressions::GetUserExpression( 10321 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language, 10322 Expression::ResultType desired_type, 10323 const EvaluateExpressionOptions &options) { 10324 TargetSP target_sp = m_target_wp.lock(); 10325 if (!target_sp) 10326 return nullptr; 10327 10328 return new ClangUserExpression(*target_sp.get(), expr, prefix, language, 10329 desired_type, options); 10330} 10331 10332FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller( 10333 const CompilerType &return_type, const Address &function_address, 10334 const ValueList &arg_value_list, const char *name) { 10335 TargetSP target_sp = m_target_wp.lock(); 10336 if (!target_sp) 10337 return nullptr; 10338 10339 Process *process = target_sp->GetProcessSP().get(); 10340 if (!process) 10341 return nullptr; 10342 10343 return new ClangFunctionCaller(*process, return_type, function_address, 10344 arg_value_list, name); 10345} 10346 10347UtilityFunction * 10348ClangASTContextForExpressions::GetUtilityFunction(const char *text, 10349 const char *name) { 10350 TargetSP target_sp = m_target_wp.lock(); 10351 if (!target_sp) 10352 return nullptr; 10353 10354 return new ClangUtilityFunction(*target_sp.get(), text, name); 10355} 10356 10357PersistentExpressionState * 10358ClangASTContextForExpressions::GetPersistentExpressionState() { 10359 return m_persistent_variables.get(); 10360} 10361 10362clang::ExternalASTMerger & 10363ClangASTContextForExpressions::GetMergerUnchecked() { 10364 lldbassert(m_scratch_ast_source_ap != nullptr); 10365 return m_scratch_ast_source_ap->GetMergerUnchecked(); 10366} 10367