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