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