LLDB mainline
PDBASTParser.cpp
Go to the documentation of this file.
1//===-- PDBASTParser.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 "PDBASTParser.h"
10
11#include "SymbolFilePDB.h"
12
13#include "clang/AST/CharUnits.h"
14#include "clang/AST/Decl.h"
15#include "clang/AST/DeclCXX.h"
16
21#include "lldb/Core/Module.h"
23#include "lldb/Symbol/TypeMap.h"
26#include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h"
27#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
28#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
29#include "llvm/DebugInfo/PDB/PDBSymbol.h"
30#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
31#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
32#include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
33#include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
34#include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
35#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
36#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h"
37#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
38#include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
39#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
40#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
41
43#include <optional>
44
45using namespace lldb;
46using namespace lldb_private;
47using namespace llvm::pdb;
48
49static int TranslateUdtKind(PDB_UdtType pdb_kind) {
50 switch (pdb_kind) {
51 case PDB_UdtType::Class:
52 return llvm::to_underlying(clang::TagTypeKind::Class);
53 case PDB_UdtType::Struct:
54 return llvm::to_underlying(clang::TagTypeKind::Struct);
55 case PDB_UdtType::Union:
56 return llvm::to_underlying(clang::TagTypeKind::Union);
57 case PDB_UdtType::Interface:
58 return llvm::to_underlying(clang::TagTypeKind::Interface);
59 }
60 llvm_unreachable("unsuported PDB UDT type");
61}
62
63static lldb::Encoding TranslateBuiltinEncoding(PDB_BuiltinType type) {
64 switch (type) {
65 case PDB_BuiltinType::Float:
67 case PDB_BuiltinType::Int:
68 case PDB_BuiltinType::Long:
69 case PDB_BuiltinType::Char:
71 case PDB_BuiltinType::Bool:
72 case PDB_BuiltinType::Char16:
73 case PDB_BuiltinType::Char32:
74 case PDB_BuiltinType::UInt:
75 case PDB_BuiltinType::ULong:
76 case PDB_BuiltinType::HResult:
77 case PDB_BuiltinType::WCharT:
79 default:
81 }
82}
83
84static lldb::Encoding TranslateEnumEncoding(PDB_VariantType type) {
85 switch (type) {
86 case PDB_VariantType::Int8:
87 case PDB_VariantType::Int16:
88 case PDB_VariantType::Int32:
89 case PDB_VariantType::Int64:
91
92 case PDB_VariantType::UInt8:
93 case PDB_VariantType::UInt16:
94 case PDB_VariantType::UInt32:
95 case PDB_VariantType::UInt64:
97
98 default:
99 break;
100 }
101
102 return lldb::eEncodingSint;
103}
104
105static CompilerType
107 const PDBSymbolTypeBuiltin &pdb_type,
108 Encoding encoding, uint32_t width) {
109 clang::ASTContext &ast = clang_ast.getASTContext();
110
111 switch (pdb_type.getBuiltinType()) {
112 default:
113 break;
114 case PDB_BuiltinType::None:
115 return CompilerType();
116 case PDB_BuiltinType::Void:
117 return clang_ast.GetBasicType(eBasicTypeVoid);
118 case PDB_BuiltinType::Char:
119 return clang_ast.GetBasicType(eBasicTypeChar);
120 case PDB_BuiltinType::Bool:
121 return clang_ast.GetBasicType(eBasicTypeBool);
122 case PDB_BuiltinType::Long:
123 if (width == ast.getTypeSize(ast.LongTy))
124 return CompilerType(clang_ast.weak_from_this(),
125 ast.LongTy.getAsOpaquePtr());
126 if (width == ast.getTypeSize(ast.LongLongTy))
127 return CompilerType(clang_ast.weak_from_this(),
128 ast.LongLongTy.getAsOpaquePtr());
129 break;
130 case PDB_BuiltinType::ULong:
131 if (width == ast.getTypeSize(ast.UnsignedLongTy))
132 return CompilerType(clang_ast.weak_from_this(),
133 ast.UnsignedLongTy.getAsOpaquePtr());
134 if (width == ast.getTypeSize(ast.UnsignedLongLongTy))
135 return CompilerType(clang_ast.weak_from_this(),
136 ast.UnsignedLongLongTy.getAsOpaquePtr());
137 break;
138 case PDB_BuiltinType::WCharT:
139 if (width == ast.getTypeSize(ast.WCharTy))
140 return CompilerType(clang_ast.weak_from_this(),
141 ast.WCharTy.getAsOpaquePtr());
142 break;
143 case PDB_BuiltinType::Char16:
144 return CompilerType(clang_ast.weak_from_this(),
145 ast.Char16Ty.getAsOpaquePtr());
146 case PDB_BuiltinType::Char32:
147 return CompilerType(clang_ast.weak_from_this(),
148 ast.Char32Ty.getAsOpaquePtr());
149 case PDB_BuiltinType::Float:
150 // Note: types `long double` and `double` have same bit size in MSVC and
151 // there is no information in the PDB to distinguish them. So when falling
152 // back to default search, the compiler type of `long double` will be
153 // represented by the one generated for `double`.
154 break;
155 }
156 // If there is no match on PDB_BuiltinType, fall back to default search by
157 // encoding and width only
158 return clang_ast.GetBuiltinTypeForEncodingAndBitSize(encoding, width);
159}
160
161static ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin &pdb_type,
162 CompilerType &compiler_type) {
163 PDB_BuiltinType kind = pdb_type.getBuiltinType();
164 switch (kind) {
165 default:
166 break;
167 case PDB_BuiltinType::Currency:
168 return ConstString("CURRENCY");
169 case PDB_BuiltinType::Date:
170 return ConstString("DATE");
171 case PDB_BuiltinType::Variant:
172 return ConstString("VARIANT");
173 case PDB_BuiltinType::Complex:
174 return ConstString("complex");
175 case PDB_BuiltinType::Bitfield:
176 return ConstString("bitfield");
177 case PDB_BuiltinType::BSTR:
178 return ConstString("BSTR");
179 case PDB_BuiltinType::HResult:
180 return ConstString("HRESULT");
181 case PDB_BuiltinType::BCD:
182 return ConstString("BCD");
183 case PDB_BuiltinType::Char16:
184 return ConstString("char16_t");
185 case PDB_BuiltinType::Char32:
186 return ConstString("char32_t");
187 case PDB_BuiltinType::None:
188 return ConstString("...");
189 }
190 return compiler_type.GetTypeName();
191}
192
193static bool AddSourceInfoToDecl(const PDBSymbol &symbol, Declaration &decl) {
194 auto &raw_sym = symbol.getRawSymbol();
195 auto first_line_up = raw_sym.getSrcLineOnTypeDefn();
196
197 if (!first_line_up) {
198 auto lines_up = symbol.getSession().findLineNumbersByAddress(
199 raw_sym.getVirtualAddress(), raw_sym.getLength());
200 if (!lines_up)
201 return false;
202 first_line_up = lines_up->getNext();
203 if (!first_line_up)
204 return false;
205 }
206 uint32_t src_file_id = first_line_up->getSourceFileId();
207 auto src_file_up = symbol.getSession().getSourceFileById(src_file_id);
208 if (!src_file_up)
209 return false;
210
211 FileSpec spec(src_file_up->getFileName());
212 decl.SetFile(spec);
213 decl.SetColumn(first_line_up->getColumnNumber());
214 decl.SetLine(first_line_up->getLineNumber());
215 return true;
216}
217
218static AccessType TranslateMemberAccess(PDB_MemberAccess access) {
219 switch (access) {
220 case PDB_MemberAccess::Private:
221 return eAccessPrivate;
222 case PDB_MemberAccess::Protected:
223 return eAccessProtected;
224 case PDB_MemberAccess::Public:
225 return eAccessPublic;
226 }
227 return eAccessNone;
228}
229
230static clang::MSInheritanceAttr::Spelling
231GetMSInheritance(const PDBSymbolTypeUDT &udt) {
232 int base_count = 0;
233 bool has_virtual = false;
234
235 auto bases_enum = udt.findAllChildren<PDBSymbolTypeBaseClass>();
236 if (bases_enum) {
237 while (auto base = bases_enum->getNext()) {
238 base_count++;
239 has_virtual |= base->isVirtualBaseClass();
240 }
241 }
242
243 if (has_virtual)
244 return clang::MSInheritanceAttr::Keyword_virtual_inheritance;
245 if (base_count > 1)
246 return clang::MSInheritanceAttr::Keyword_multiple_inheritance;
247 return clang::MSInheritanceAttr::Keyword_single_inheritance;
248}
249
250static std::unique_ptr<llvm::pdb::PDBSymbol>
251GetClassOrFunctionParent(const llvm::pdb::PDBSymbol &symbol) {
252 const IPDBSession &session = symbol.getSession();
253 const IPDBRawSymbol &raw = symbol.getRawSymbol();
254 auto tag = symbol.getSymTag();
255
256 // For items that are nested inside of a class, return the class that it is
257 // nested inside of.
258 // Note that only certain items can be nested inside of classes.
259 switch (tag) {
260 case PDB_SymType::Function:
261 case PDB_SymType::Data:
262 case PDB_SymType::UDT:
263 case PDB_SymType::Enum:
264 case PDB_SymType::FunctionSig:
265 case PDB_SymType::Typedef:
266 case PDB_SymType::BaseClass:
267 case PDB_SymType::VTable: {
268 auto class_parent_id = raw.getClassParentId();
269 if (auto class_parent = session.getSymbolById(class_parent_id))
270 return class_parent;
271 break;
272 }
273 default:
274 break;
275 }
276
277 // Otherwise, if it is nested inside of a function, return the function.
278 // Note that only certain items can be nested inside of functions.
279 switch (tag) {
280 case PDB_SymType::Block:
281 case PDB_SymType::Data: {
282 auto lexical_parent_id = raw.getLexicalParentId();
283 auto lexical_parent = session.getSymbolById(lexical_parent_id);
284 if (!lexical_parent)
285 return nullptr;
286
287 auto lexical_parent_tag = lexical_parent->getSymTag();
288 if (lexical_parent_tag == PDB_SymType::Function)
289 return lexical_parent;
290 if (lexical_parent_tag == PDB_SymType::Exe)
291 return nullptr;
292
293 return GetClassOrFunctionParent(*lexical_parent);
294 }
295 default:
296 return nullptr;
297 }
298}
299
300static clang::NamedDecl *
301GetDeclFromContextByName(const clang::ASTContext &ast,
302 const clang::DeclContext &decl_context,
303 llvm::StringRef name) {
304 clang::IdentifierInfo &ident = ast.Idents.get(name);
305 clang::DeclarationName decl_name = ast.DeclarationNames.getIdentifier(&ident);
306 clang::DeclContext::lookup_result result = decl_context.lookup(decl_name);
307 if (result.empty())
308 return nullptr;
309
310 return *result.begin();
311}
312
313static bool IsAnonymousNamespaceName(llvm::StringRef name) {
314 return name == "`anonymous namespace'" || name == "`anonymous-namespace'";
315}
316
317static clang::CallingConv TranslateCallingConvention(PDB_CallingConv pdb_cc) {
318 switch (pdb_cc) {
319 case llvm::codeview::CallingConvention::NearC:
320 return clang::CC_C;
321 case llvm::codeview::CallingConvention::NearStdCall:
322 return clang::CC_X86StdCall;
323 case llvm::codeview::CallingConvention::NearFast:
324 return clang::CC_X86FastCall;
325 case llvm::codeview::CallingConvention::ThisCall:
326 return clang::CC_X86ThisCall;
327 case llvm::codeview::CallingConvention::NearVector:
328 return clang::CC_X86VectorCall;
329 case llvm::codeview::CallingConvention::NearPascal:
330 return clang::CC_X86Pascal;
331 default:
332 assert(false && "Unknown calling convention");
333 return clang::CC_C;
334 }
335}
336
338
340
341// DebugInfoASTParser interface
342
344 Declaration decl;
345 switch (type.getSymTag()) {
346 case PDB_SymType::BaseClass: {
347 auto symbol_file = m_ast.GetSymbolFile();
348 if (!symbol_file)
349 return nullptr;
350
351 auto ty = symbol_file->ResolveTypeUID(type.getRawSymbol().getTypeId());
352 return ty ? ty->shared_from_this() : nullptr;
353 } break;
354 case PDB_SymType::UDT: {
355 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type);
356 assert(udt);
357
358 // Note that, unnamed UDT being typedef-ed is generated as a UDT symbol
359 // other than a Typedef symbol in PDB. For example,
360 // typedef union { short Row; short Col; } Union;
361 // is generated as a named UDT in PDB:
362 // union Union { short Row; short Col; }
363 // Such symbols will be handled here.
364
365 // Some UDT with trival ctor has zero length. Just ignore.
366 if (udt->getLength() == 0)
367 return nullptr;
368
369 // Ignore unnamed-tag UDTs.
370 std::string name =
371 std::string(MSVCUndecoratedNameParser::DropScope(udt->getName()));
372 if (name.empty())
373 return nullptr;
374
375 auto decl_context = GetDeclContextContainingSymbol(type);
376
377 // Check if such an UDT already exists in the current context.
378 // This may occur with const or volatile types. There are separate type
379 // symbols in PDB for types with const or volatile modifiers, but we need
380 // to create only one declaration for them all.
381 Type::ResolveState type_resolve_state;
382 CompilerType clang_type = m_ast.GetTypeForIdentifier<clang::CXXRecordDecl>(
383 m_ast.getASTContext(), name, decl_context);
384 if (!clang_type.IsValid()) {
385 auto tag_type_kind = TranslateUdtKind(udt->getUdtKind());
386
387 ClangASTMetadata metadata;
388 metadata.SetUserID(type.getSymIndexId());
389 metadata.SetIsDynamicCXXType(false);
390
391 clang_type = m_ast.CreateRecordType(
392 decl_context, OptionalClangModuleID(), name, tag_type_kind,
394 assert(clang_type.IsValid());
395
396 auto record_decl =
397 m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
398 assert(record_decl);
399 m_uid_to_decl[type.getSymIndexId()] = record_decl;
400
401 auto inheritance_attr = clang::MSInheritanceAttr::CreateImplicit(
402 m_ast.getASTContext(), GetMSInheritance(*udt));
403 record_decl->addAttr(inheritance_attr);
404
406
407 auto children = udt->findAllChildren();
408 if (!children || children->getChildCount() == 0) {
409 // PDB does not have symbol of forwarder. We assume we get an udt w/o
410 // any fields. Just complete it at this point.
412
414 false);
415
416 type_resolve_state = Type::ResolveState::Full;
417 } else {
418 // Add the type to the forward declarations. It will help us to avoid
419 // an endless recursion in CompleteTypeFromUdt function.
420 m_forward_decl_to_uid[record_decl] = type.getSymIndexId();
421
423 true);
424
425 type_resolve_state = Type::ResolveState::Forward;
426 }
427 } else
428 type_resolve_state = Type::ResolveState::Forward;
429
430 if (udt->isConstType())
431 clang_type = clang_type.AddConstModifier();
432
433 if (udt->isVolatileType())
434 clang_type = clang_type.AddVolatileModifier();
435
436 AddSourceInfoToDecl(type, decl);
437 return m_ast.GetSymbolFile()->MakeType(
438 type.getSymIndexId(), ConstString(name), udt->getLength(), nullptr,
440 type_resolve_state);
441 } break;
442 case PDB_SymType::Enum: {
443 auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type);
444 assert(enum_type);
445
446 std::string name =
447 std::string(MSVCUndecoratedNameParser::DropScope(enum_type->getName()));
448 auto decl_context = GetDeclContextContainingSymbol(type);
449 uint64_t bytes = enum_type->getLength();
450
451 // Check if such an enum already exists in the current context
452 CompilerType ast_enum = m_ast.GetTypeForIdentifier<clang::EnumDecl>(
453 m_ast.getASTContext(), name, decl_context);
454 if (!ast_enum.IsValid()) {
455 auto underlying_type_up = enum_type->getUnderlyingType();
456 if (!underlying_type_up)
457 return nullptr;
458
459 lldb::Encoding encoding =
460 TranslateBuiltinEncoding(underlying_type_up->getBuiltinType());
461 // FIXME: Type of underlying builtin is always `Int`. We correct it with
462 // the very first enumerator's encoding if any.
463 auto first_child = enum_type->findOneChild<PDBSymbolData>();
464 if (first_child)
465 encoding = TranslateEnumEncoding(first_child->getValue().Type);
466
467 CompilerType builtin_type;
468 if (bytes > 0)
470 m_ast, *underlying_type_up, encoding, bytes * 8);
471 else
472 builtin_type = m_ast.GetBasicType(eBasicTypeInt);
473
474 // FIXME: PDB does not have information about scoped enumeration (Enum
475 // Class). Set it false for now.
476 bool isScoped = false;
477
478 ast_enum = m_ast.CreateEnumerationType(name, decl_context,
479 OptionalClangModuleID(), decl,
480 builtin_type, isScoped);
481
482 auto enum_decl = TypeSystemClang::GetAsEnumDecl(ast_enum);
483 assert(enum_decl);
484 m_uid_to_decl[type.getSymIndexId()] = enum_decl;
485
486 auto enum_values = enum_type->findAllChildren<PDBSymbolData>();
487 if (enum_values) {
488 while (auto enum_value = enum_values->getNext()) {
489 if (enum_value->getDataKind() != PDB_DataKind::Constant)
490 continue;
491 AddEnumValue(ast_enum, *enum_value);
492 }
493 }
494
497 }
498
499 if (enum_type->isConstType())
500 ast_enum = ast_enum.AddConstModifier();
501
502 if (enum_type->isVolatileType())
503 ast_enum = ast_enum.AddVolatileModifier();
504
505 AddSourceInfoToDecl(type, decl);
506 return m_ast.GetSymbolFile()->MakeType(
507 type.getSymIndexId(), ConstString(name), bytes, nullptr,
510 } break;
511 case PDB_SymType::Typedef: {
512 auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type);
513 assert(type_def);
514
515 SymbolFile *symbol_file = m_ast.GetSymbolFile();
516 if (!symbol_file)
517 return nullptr;
518
519 lldb_private::Type *target_type =
520 symbol_file->ResolveTypeUID(type_def->getTypeId());
521 if (!target_type)
522 return nullptr;
523
524 std::string name =
525 std::string(MSVCUndecoratedNameParser::DropScope(type_def->getName()));
526 auto decl_ctx = GetDeclContextContainingSymbol(type);
527
528 // Check if such a typedef already exists in the current context
529 CompilerType ast_typedef =
530 m_ast.GetTypeForIdentifier<clang::TypedefNameDecl>(
531 m_ast.getASTContext(), name, decl_ctx);
532 if (!ast_typedef.IsValid()) {
533 CompilerType target_ast_type = target_type->GetFullCompilerType();
534
535 ast_typedef = target_ast_type.CreateTypedef(
536 name.c_str(), m_ast.CreateDeclContext(decl_ctx), 0);
537 if (!ast_typedef)
538 return nullptr;
539
540 auto typedef_decl = TypeSystemClang::GetAsTypedefDecl(ast_typedef);
541 assert(typedef_decl);
542 m_uid_to_decl[type.getSymIndexId()] = typedef_decl;
543 }
544
545 if (type_def->isConstType())
546 ast_typedef = ast_typedef.AddConstModifier();
547
548 if (type_def->isVolatileType())
549 ast_typedef = ast_typedef.AddVolatileModifier();
550
551 AddSourceInfoToDecl(type, decl);
552 std::optional<uint64_t> size;
553 if (type_def->getLength())
554 size = type_def->getLength();
555 return m_ast.GetSymbolFile()->MakeType(
556 type_def->getSymIndexId(), ConstString(name), size, nullptr,
559 } break;
560 case PDB_SymType::Function:
561 case PDB_SymType::FunctionSig: {
562 std::string name;
563 PDBSymbolTypeFunctionSig *func_sig = nullptr;
564 if (auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(&type)) {
565 if (pdb_func->isCompilerGenerated())
566 return nullptr;
567
568 auto sig = pdb_func->getSignature();
569 if (!sig)
570 return nullptr;
571 func_sig = sig.release();
572 // Function type is named.
573 name = std::string(
574 MSVCUndecoratedNameParser::DropScope(pdb_func->getName()));
575 } else if (auto pdb_func_sig =
576 llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) {
577 func_sig = const_cast<PDBSymbolTypeFunctionSig *>(pdb_func_sig);
578 } else
579 llvm_unreachable("Unexpected PDB symbol!");
580
581 auto arg_enum = func_sig->getArguments();
582 uint32_t num_args = arg_enum->getChildCount();
583 std::vector<CompilerType> arg_list;
584
585 bool is_variadic = func_sig->isCVarArgs();
586 // Drop last variadic argument.
587 if (is_variadic)
588 --num_args;
589 for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) {
590 auto arg = arg_enum->getChildAtIndex(arg_idx);
591 if (!arg)
592 break;
593
594 SymbolFile *symbol_file = m_ast.GetSymbolFile();
595 if (!symbol_file)
596 return nullptr;
597
598 lldb_private::Type *arg_type =
599 symbol_file->ResolveTypeUID(arg->getSymIndexId());
600 // If there's some error looking up one of the dependent types of this
601 // function signature, bail.
602 if (!arg_type)
603 return nullptr;
604 CompilerType arg_ast_type = arg_type->GetFullCompilerType();
605 arg_list.push_back(arg_ast_type);
606 }
607 lldbassert(arg_list.size() <= num_args);
608
609 auto pdb_return_type = func_sig->getReturnType();
610 SymbolFile *symbol_file = m_ast.GetSymbolFile();
611 if (!symbol_file)
612 return nullptr;
613
614 lldb_private::Type *return_type =
615 symbol_file->ResolveTypeUID(pdb_return_type->getSymIndexId());
616 // If there's some error looking up one of the dependent types of this
617 // function signature, bail.
618 if (!return_type)
619 return nullptr;
620 CompilerType return_ast_type = return_type->GetFullCompilerType();
621 uint32_t type_quals = 0;
622 if (func_sig->isConstType())
623 type_quals |= clang::Qualifiers::Const;
624 if (func_sig->isVolatileType())
625 type_quals |= clang::Qualifiers::Volatile;
626 auto cc = TranslateCallingConvention(func_sig->getCallingConvention());
627 CompilerType func_sig_ast_type = m_ast.CreateFunctionType(
628 return_ast_type, arg_list, is_variadic, type_quals, cc);
629
630 AddSourceInfoToDecl(type, decl);
631 return m_ast.GetSymbolFile()->MakeType(
632 type.getSymIndexId(), ConstString(name), std::nullopt, nullptr,
634 func_sig_ast_type, lldb_private::Type::ResolveState::Full);
635 } break;
636 case PDB_SymType::ArrayType: {
637 auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type);
638 assert(array_type);
639 uint32_t num_elements = array_type->getCount();
640 uint32_t element_uid = array_type->getElementTypeId();
641 std::optional<uint64_t> bytes;
642 if (uint64_t size = array_type->getLength())
643 bytes = size;
644
645 SymbolFile *symbol_file = m_ast.GetSymbolFile();
646 if (!symbol_file)
647 return nullptr;
648
649 // If array rank > 0, PDB gives the element type at N=0. So element type
650 // will parsed in the order N=0, N=1,..., N=rank sequentially.
651 lldb_private::Type *element_type = symbol_file->ResolveTypeUID(element_uid);
652 if (!element_type)
653 return nullptr;
654
655 CompilerType element_ast_type = element_type->GetForwardCompilerType();
656 // If element type is UDT, it needs to be complete.
657 if (TypeSystemClang::IsCXXClassType(element_ast_type) &&
658 !element_ast_type.GetCompleteType()) {
661 } else {
662 // We are not able to start definition.
663 return nullptr;
664 }
665 }
666 CompilerType array_ast_type = m_ast.CreateArrayType(
667 element_ast_type, num_elements, /*is_gnu_vector*/ false);
668 TypeSP type_sp = m_ast.GetSymbolFile()->MakeType(
669 array_type->getSymIndexId(), ConstString(), bytes, nullptr,
672 type_sp->SetEncodingType(element_type);
673 return type_sp;
674 } break;
675 case PDB_SymType::BuiltinType: {
676 auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type);
677 assert(builtin_type);
678 PDB_BuiltinType builtin_kind = builtin_type->getBuiltinType();
679 if (builtin_kind == PDB_BuiltinType::None)
680 return nullptr;
681
682 std::optional<uint64_t> bytes;
683 if (uint64_t size = builtin_type->getLength())
684 bytes = size;
685 Encoding encoding = TranslateBuiltinEncoding(builtin_kind);
687 m_ast, *builtin_type, encoding, bytes.value_or(0) * 8);
688
689 if (builtin_type->isConstType())
690 builtin_ast_type = builtin_ast_type.AddConstModifier();
691
692 if (builtin_type->isVolatileType())
693 builtin_ast_type = builtin_ast_type.AddVolatileModifier();
694
695 auto type_name = GetPDBBuiltinTypeName(*builtin_type, builtin_ast_type);
696
697 return m_ast.GetSymbolFile()->MakeType(
698 builtin_type->getSymIndexId(), type_name, bytes, nullptr,
701 } break;
702 case PDB_SymType::PointerType: {
703 auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type);
704 assert(pointer_type);
705
706 SymbolFile *symbol_file = m_ast.GetSymbolFile();
707 if (!symbol_file)
708 return nullptr;
709
710 Type *pointee_type = symbol_file->ResolveTypeUID(
711 pointer_type->getPointeeType()->getSymIndexId());
712 if (!pointee_type)
713 return nullptr;
714
715 if (pointer_type->isPointerToDataMember() ||
716 pointer_type->isPointerToMemberFunction()) {
717 auto class_parent_uid = pointer_type->getRawSymbol().getClassParentId();
718 auto class_parent_type = symbol_file->ResolveTypeUID(class_parent_uid);
719 assert(class_parent_type);
720
721 CompilerType pointer_ast_type;
723 class_parent_type->GetLayoutCompilerType(),
724 pointee_type->GetForwardCompilerType());
725 assert(pointer_ast_type);
726
727 return m_ast.GetSymbolFile()->MakeType(
728 pointer_type->getSymIndexId(), ConstString(),
729 pointer_type->getLength(), nullptr, LLDB_INVALID_UID,
730 lldb_private::Type::eEncodingIsUID, decl, pointer_ast_type,
732 }
733
734 CompilerType pointer_ast_type;
735 pointer_ast_type = pointee_type->GetFullCompilerType();
736 if (pointer_type->isReference())
737 pointer_ast_type = pointer_ast_type.GetLValueReferenceType();
738 else if (pointer_type->isRValueReference())
739 pointer_ast_type = pointer_ast_type.GetRValueReferenceType();
740 else
741 pointer_ast_type = pointer_ast_type.GetPointerType();
742
743 if (pointer_type->isConstType())
744 pointer_ast_type = pointer_ast_type.AddConstModifier();
745
746 if (pointer_type->isVolatileType())
747 pointer_ast_type = pointer_ast_type.AddVolatileModifier();
748
749 if (pointer_type->isRestrictedType())
750 pointer_ast_type = pointer_ast_type.AddRestrictModifier();
751
752 return m_ast.GetSymbolFile()->MakeType(
753 pointer_type->getSymIndexId(), ConstString(), pointer_type->getLength(),
756 } break;
757 default:
758 break;
759 }
760 return nullptr;
761}
762
764 lldb_private::CompilerType &compiler_type) {
765 if (GetClangASTImporter().CanImport(compiler_type))
766 return GetClangASTImporter().CompleteType(compiler_type);
767
768 // Remove the type from the forward declarations to avoid
769 // an endless recursion for types like a linked list.
770 clang::CXXRecordDecl *record_decl =
771 m_ast.GetAsCXXRecordDecl(compiler_type.GetOpaqueQualType());
772 auto uid_it = m_forward_decl_to_uid.find(record_decl);
773 if (uid_it == m_forward_decl_to_uid.end())
774 return true;
775
776 auto symbol_file = static_cast<SymbolFilePDB *>(
777 m_ast.GetSymbolFile()->GetBackingSymbolFile());
778 if (!symbol_file)
779 return false;
780
781 std::unique_ptr<PDBSymbol> symbol =
782 symbol_file->GetPDBSession().getSymbolById(uid_it->getSecond());
783 if (!symbol)
784 return false;
785
786 m_forward_decl_to_uid.erase(uid_it);
787
789 false);
790
791 switch (symbol->getSymTag()) {
792 case PDB_SymType::UDT: {
793 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(symbol.get());
794 if (!udt)
795 return false;
796
797 return CompleteTypeFromUDT(*symbol_file, compiler_type, *udt);
798 }
799 default:
800 llvm_unreachable("not a forward clang type decl!");
801 }
802}
803
804clang::Decl *
805PDBASTParser::GetDeclForSymbol(const llvm::pdb::PDBSymbol &symbol) {
806 uint32_t sym_id = symbol.getSymIndexId();
807 auto it = m_uid_to_decl.find(sym_id);
808 if (it != m_uid_to_decl.end())
809 return it->second;
810
811 auto symbol_file = static_cast<SymbolFilePDB *>(
812 m_ast.GetSymbolFile()->GetBackingSymbolFile());
813 if (!symbol_file)
814 return nullptr;
815
816 // First of all, check if the symbol is a member of a class. Resolve the full
817 // class type and return the declaration from the cache if so.
818 auto tag = symbol.getSymTag();
819 if (tag == PDB_SymType::Data || tag == PDB_SymType::Function) {
820 const IPDBSession &session = symbol.getSession();
821 const IPDBRawSymbol &raw = symbol.getRawSymbol();
822
823 auto class_parent_id = raw.getClassParentId();
824 if (std::unique_ptr<PDBSymbol> class_parent =
825 session.getSymbolById(class_parent_id)) {
826 auto class_parent_type = symbol_file->ResolveTypeUID(class_parent_id);
827 if (!class_parent_type)
828 return nullptr;
829
830 CompilerType class_parent_ct = class_parent_type->GetFullCompilerType();
831
832 // Look a declaration up in the cache after completing the class
833 clang::Decl *decl = m_uid_to_decl.lookup(sym_id);
834 if (decl)
835 return decl;
836
837 // A declaration was not found in the cache. It means that the symbol
838 // has the class parent, but the class doesn't have the symbol in its
839 // children list.
840 if (auto func = llvm::dyn_cast_or_null<PDBSymbolFunc>(&symbol)) {
841 // Try to find a class child method with the same RVA and use its
842 // declaration if found.
843 if (uint32_t rva = func->getRelativeVirtualAddress()) {
844 if (std::unique_ptr<ConcreteSymbolEnumerator<PDBSymbolFunc>>
845 methods_enum =
846 class_parent->findAllChildren<PDBSymbolFunc>()) {
847 while (std::unique_ptr<PDBSymbolFunc> method =
848 methods_enum->getNext()) {
849 if (method->getRelativeVirtualAddress() == rva) {
850 decl = m_uid_to_decl.lookup(method->getSymIndexId());
851 if (decl)
852 break;
853 }
854 }
855 }
856 }
857
858 // If no class methods with the same RVA were found, then create a new
859 // method. It is possible for template methods.
860 if (!decl)
861 decl = AddRecordMethod(*symbol_file, class_parent_ct, *func);
862 }
863
864 if (decl)
865 m_uid_to_decl[sym_id] = decl;
866
867 return decl;
868 }
869 }
870
871 // If we are here, then the symbol is not belonging to a class and is not
872 // contained in the cache. So create a declaration for it.
873 switch (symbol.getSymTag()) {
874 case PDB_SymType::Data: {
875 auto data = llvm::dyn_cast<PDBSymbolData>(&symbol);
876 assert(data);
877
878 auto decl_context = GetDeclContextContainingSymbol(symbol);
879 assert(decl_context);
880
881 // May be the current context is a class really, but we haven't found
882 // any class parent. This happens e.g. in the case of class static
883 // variables - they has two symbols, one is a child of the class when
884 // another is a child of the exe. So always complete the parent and use
885 // an existing declaration if possible.
886 if (auto parent_decl = llvm::dyn_cast_or_null<clang::TagDecl>(decl_context))
887 m_ast.GetCompleteDecl(parent_decl);
888
889 std::string name =
890 std::string(MSVCUndecoratedNameParser::DropScope(data->getName()));
891
892 // Check if the current context already contains the symbol with the name.
893 clang::Decl *decl =
894 GetDeclFromContextByName(m_ast.getASTContext(), *decl_context, name);
895 if (!decl) {
896 auto type = symbol_file->ResolveTypeUID(data->getTypeId());
897 if (!type)
898 return nullptr;
899
900 decl = m_ast.CreateVariableDeclaration(
901 decl_context, OptionalClangModuleID(), name.c_str(),
902 ClangUtil::GetQualType(type->GetLayoutCompilerType()));
903 }
904
905 m_uid_to_decl[sym_id] = decl;
906
907 return decl;
908 }
909 case PDB_SymType::Function: {
910 auto func = llvm::dyn_cast<PDBSymbolFunc>(&symbol);
911 assert(func);
912
913 auto decl_context = GetDeclContextContainingSymbol(symbol);
914 assert(decl_context);
915
916 std::string name =
917 std::string(MSVCUndecoratedNameParser::DropScope(func->getName()));
918
919 Type *type = symbol_file->ResolveTypeUID(sym_id);
920 if (!type)
921 return nullptr;
922
923 auto storage = func->isStatic() ? clang::StorageClass::SC_Static
924 : clang::StorageClass::SC_None;
925
926 auto decl = m_ast.CreateFunctionDeclaration(
927 decl_context, OptionalClangModuleID(), name,
928 type->GetForwardCompilerType(), storage, func->hasInlineAttribute(),
929 /*asm_label=*/{});
930
931 std::vector<clang::ParmVarDecl *> params;
932 if (std::unique_ptr<PDBSymbolTypeFunctionSig> sig = func->getSignature()) {
934 arg_enum = sig->findAllChildren<PDBSymbolTypeFunctionArg>()) {
935 while (std::unique_ptr<PDBSymbolTypeFunctionArg> arg =
936 arg_enum->getNext()) {
937 Type *arg_type = symbol_file->ResolveTypeUID(arg->getTypeId());
938 if (!arg_type)
939 continue;
940
941 clang::ParmVarDecl *param = m_ast.CreateParameterDeclaration(
942 decl, OptionalClangModuleID(), nullptr,
943 arg_type->GetForwardCompilerType(), clang::SC_None, true);
944 if (param)
945 params.push_back(param);
946 }
947 }
948 }
949 if (params.size() && decl)
950 decl->setParams(params);
951
952 m_uid_to_decl[sym_id] = decl;
953
954 return decl;
955 }
956 default: {
957 // It's not a variable and not a function, check if it's a type
958 Type *type = symbol_file->ResolveTypeUID(sym_id);
959 if (!type)
960 return nullptr;
961
962 return m_uid_to_decl.lookup(sym_id);
963 }
964 }
965}
966
967clang::DeclContext *
968PDBASTParser::GetDeclContextForSymbol(const llvm::pdb::PDBSymbol &symbol) {
969 if (symbol.getSymTag() == PDB_SymType::Function) {
970 clang::DeclContext *result =
971 llvm::dyn_cast_or_null<clang::FunctionDecl>(GetDeclForSymbol(symbol));
972
973 if (result)
974 m_decl_context_to_uid[result] = symbol.getSymIndexId();
975
976 return result;
977 }
978
979 auto symbol_file = static_cast<SymbolFilePDB *>(
980 m_ast.GetSymbolFile()->GetBackingSymbolFile());
981 if (!symbol_file)
982 return nullptr;
983
984 auto type = symbol_file->ResolveTypeUID(symbol.getSymIndexId());
985 if (!type)
986 return nullptr;
987
988 clang::DeclContext *result =
989 m_ast.GetDeclContextForType(type->GetForwardCompilerType());
990
991 if (result)
992 m_decl_context_to_uid[result] = symbol.getSymIndexId();
993
994 return result;
995}
996
998 const llvm::pdb::PDBSymbol &symbol) {
999 auto parent = GetClassOrFunctionParent(symbol);
1000 while (parent) {
1001 if (auto parent_context = GetDeclContextForSymbol(*parent))
1002 return parent_context;
1003
1004 parent = GetClassOrFunctionParent(*parent);
1005 }
1006
1007 // We can't find any class or function parent of the symbol. So analyze
1008 // the full symbol name. The symbol may be belonging to a namespace
1009 // or function (or even to a class if it's e.g. a static variable symbol).
1010
1011 // TODO: Make clang to emit full names for variables in namespaces
1012 // (as MSVC does)
1013
1014 std::string name(symbol.getRawSymbol().getName());
1015 MSVCUndecoratedNameParser parser(name);
1016 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers();
1017 if (specs.empty())
1018 return m_ast.GetTranslationUnitDecl();
1019
1020 auto symbol_file = static_cast<SymbolFilePDB *>(
1021 m_ast.GetSymbolFile()->GetBackingSymbolFile());
1022 if (!symbol_file)
1023 return m_ast.GetTranslationUnitDecl();
1024
1025 auto global = symbol_file->GetPDBSession().getGlobalScope();
1026 if (!global)
1027 return m_ast.GetTranslationUnitDecl();
1028
1029 bool has_type_or_function_parent = false;
1030 clang::DeclContext *curr_context = m_ast.GetTranslationUnitDecl();
1031 for (std::size_t i = 0; i < specs.size() - 1; i++) {
1032 // Check if there is a function or a type with the current context's name.
1033 if (std::unique_ptr<IPDBEnumSymbols> children_enum = global->findChildren(
1034 PDB_SymType::None, specs[i].GetFullName(), NS_CaseSensitive)) {
1035 while (IPDBEnumChildren<PDBSymbol>::ChildTypePtr child =
1036 children_enum->getNext()) {
1037 if (clang::DeclContext *child_context =
1038 GetDeclContextForSymbol(*child)) {
1039 // Note that `GetDeclContextForSymbol' retrieves
1040 // a declaration context for functions and types only,
1041 // so if we are here then `child_context' is guaranteed
1042 // a function or a type declaration context.
1043 has_type_or_function_parent = true;
1044 curr_context = child_context;
1045 }
1046 }
1047 }
1048
1049 // If there were no functions or types above then retrieve a namespace with
1050 // the current context's name. There can be no namespaces inside a function
1051 // or a type. We check it to avoid fake namespaces such as `__l2':
1052 // `N0::N1::CClass::PrivateFunc::__l2::InnerFuncStruct'
1053 if (!has_type_or_function_parent) {
1054 std::string namespace_name = std::string(specs[i].GetBaseName());
1055 const char *namespace_name_c_str =
1056 IsAnonymousNamespaceName(namespace_name) ? nullptr
1057 : namespace_name.data();
1058 clang::NamespaceDecl *namespace_decl =
1059 m_ast.GetUniqueNamespaceDeclaration(
1060 namespace_name_c_str, curr_context, OptionalClangModuleID());
1061
1062 m_parent_to_namespaces[curr_context].insert(namespace_decl);
1063 m_namespaces.insert(namespace_decl);
1064
1065 curr_context = namespace_decl;
1066 }
1067 }
1068
1069 return curr_context;
1070}
1071
1073 const clang::DeclContext *decl_context) {
1074 auto symbol_file = static_cast<SymbolFilePDB *>(
1075 m_ast.GetSymbolFile()->GetBackingSymbolFile());
1076 if (!symbol_file)
1077 return;
1078
1079 IPDBSession &session = symbol_file->GetPDBSession();
1080 auto symbol_up =
1081 session.getSymbolById(m_decl_context_to_uid.lookup(decl_context));
1082 auto global_up = session.getGlobalScope();
1083
1084 PDBSymbol *symbol;
1085 if (symbol_up)
1086 symbol = symbol_up.get();
1087 else if (global_up)
1088 symbol = global_up.get();
1089 else
1090 return;
1091
1092 if (auto children = symbol->findAllChildren())
1093 while (auto child = children->getNext())
1094 GetDeclForSymbol(*child);
1095}
1096
1097clang::NamespaceDecl *
1098PDBASTParser::FindNamespaceDecl(const clang::DeclContext *parent,
1099 llvm::StringRef name) {
1100 NamespacesSet *set;
1101 if (parent) {
1102 auto pit = m_parent_to_namespaces.find(parent);
1103 if (pit == m_parent_to_namespaces.end())
1104 return nullptr;
1105
1106 set = &pit->second;
1107 } else {
1108 set = &m_namespaces;
1109 }
1110 assert(set);
1111
1112 for (clang::NamespaceDecl *namespace_decl : *set)
1113 if (namespace_decl->getName() == name)
1114 return namespace_decl;
1115
1116 for (clang::NamespaceDecl *namespace_decl : *set)
1117 if (namespace_decl->isAnonymousNamespace())
1118 return FindNamespaceDecl(namespace_decl, name);
1119
1120 return nullptr;
1121}
1122
1124 const PDBSymbolData &enum_value) {
1125 Declaration decl;
1126 Variant v = enum_value.getValue();
1127 std::string name =
1128 std::string(MSVCUndecoratedNameParser::DropScope(enum_value.getName()));
1129 uint64_t raw_value;
1130 switch (v.Type) {
1131 case PDB_VariantType::Int8:
1132 raw_value = v.Value.Int8;
1133 break;
1134 case PDB_VariantType::Int16:
1135 raw_value = v.Value.Int16;
1136 break;
1137 case PDB_VariantType::Int32:
1138 raw_value = v.Value.Int32;
1139 break;
1140 case PDB_VariantType::Int64:
1141 raw_value = v.Value.Int64;
1142 break;
1143 case PDB_VariantType::UInt8:
1144 raw_value = v.Value.UInt8;
1145 break;
1146 case PDB_VariantType::UInt16:
1147 raw_value = v.Value.UInt16;
1148 break;
1149 case PDB_VariantType::UInt32:
1150 raw_value = v.Value.UInt32;
1151 break;
1152 case PDB_VariantType::UInt64:
1153 raw_value = v.Value.UInt64;
1154 break;
1155 default:
1156 return false;
1157 }
1158 CompilerType underlying_type = m_ast.GetEnumerationIntegerType(enum_type);
1159 uint32_t byte_size = m_ast.getASTContext().getTypeSize(
1160 ClangUtil::GetQualType(underlying_type));
1161 auto enum_constant_decl = m_ast.AddEnumerationValueToEnumerationType(
1162 enum_type, decl, name.c_str(), raw_value, byte_size * 8);
1163 if (!enum_constant_decl)
1164 return false;
1165
1166 m_uid_to_decl[enum_value.getSymIndexId()] = enum_constant_decl;
1167
1168 return true;
1169}
1170
1172 lldb_private::SymbolFile &symbol_file,
1173 lldb_private::CompilerType &compiler_type,
1174 llvm::pdb::PDBSymbolTypeUDT &udt) {
1175 ClangASTImporter::LayoutInfo layout_info;
1176 layout_info.bit_size = udt.getLength() * 8;
1177
1178 auto nested_enums = udt.findAllChildren<PDBSymbolTypeUDT>();
1179 if (nested_enums)
1180 while (auto nested = nested_enums->getNext())
1181 symbol_file.ResolveTypeUID(nested->getSymIndexId());
1182
1183 auto bases_enum = udt.findAllChildren<PDBSymbolTypeBaseClass>();
1184 if (bases_enum)
1185 AddRecordBases(symbol_file, compiler_type,
1186 TranslateUdtKind(udt.getUdtKind()), *bases_enum,
1187 layout_info);
1188
1189 auto members_enum = udt.findAllChildren<PDBSymbolData>();
1190 if (members_enum)
1191 AddRecordMembers(symbol_file, compiler_type, *members_enum, layout_info);
1192
1193 auto methods_enum = udt.findAllChildren<PDBSymbolFunc>();
1194 if (methods_enum)
1195 AddRecordMethods(symbol_file, compiler_type, *methods_enum);
1196
1197 m_ast.AddMethodOverridesForCXXRecordType(compiler_type.GetOpaqueQualType());
1200
1201 clang::CXXRecordDecl *record_decl =
1202 m_ast.GetAsCXXRecordDecl(compiler_type.GetOpaqueQualType());
1203 if (!record_decl)
1204 return static_cast<bool>(compiler_type);
1205
1206 GetClangASTImporter().SetRecordLayout(record_decl, layout_info);
1207
1208 return static_cast<bool>(compiler_type);
1209}
1210
1212 lldb_private::SymbolFile &symbol_file,
1213 lldb_private::CompilerType &record_type,
1214 PDBDataSymbolEnumerator &members_enum,
1216 while (auto member = members_enum.getNext()) {
1217 if (member->isCompilerGenerated())
1218 continue;
1219
1220 auto member_name = member->getName();
1221
1222 auto member_type = symbol_file.ResolveTypeUID(member->getTypeId());
1223 if (!member_type)
1224 continue;
1225
1226 auto member_comp_type = member_type->GetLayoutCompilerType();
1227 if (!member_comp_type.GetCompleteType()) {
1228 symbol_file.GetObjectFile()->GetModule()->ReportError(
1229 ":: Class '{0}' has a member '{1}' of type '{2}' "
1230 "which does not have a complete definition.",
1231 record_type.GetTypeName().GetCString(), member_name.c_str(),
1232 member_comp_type.GetTypeName().GetCString());
1235 }
1236
1237 switch (member->getDataKind()) {
1238 case PDB_DataKind::Member: {
1239 auto location_type = member->getLocationType();
1240
1241 auto bit_size = member->getLength();
1242 if (location_type == PDB_LocType::ThisRel)
1243 bit_size *= 8;
1244
1246 record_type, member_name.c_str(), member_comp_type, bit_size);
1247 if (!decl)
1248 continue;
1249
1250 m_uid_to_decl[member->getSymIndexId()] = decl;
1251
1252 auto offset = member->getOffset() * 8;
1253 if (location_type == PDB_LocType::BitField)
1254 offset += member->getBitPosition();
1255
1256 layout_info.field_offsets.insert(std::make_pair(decl, offset));
1257
1258 break;
1259 }
1260 case PDB_DataKind::StaticMember: {
1262 record_type, member_name.c_str(), member_comp_type);
1263 if (!decl)
1264 continue;
1265
1266 // Static constant members may be a const[expr] declaration.
1267 // Query the symbol's value as the variable initializer if valid.
1268 if (member_comp_type.IsConst()) {
1269 auto value = member->getValue();
1270 if (value.Type == llvm::pdb::Empty) {
1272 "Class '{0}' has member '{1}' of type '{2}' with an unknown "
1273 "constant size.",
1274 record_type.GetTypeName(), member_name,
1275 member_comp_type.GetTypeName());
1276 continue;
1277 }
1278
1279 clang::QualType qual_type = decl->getType();
1280 unsigned type_width = m_ast.getASTContext().getIntWidth(qual_type);
1281 unsigned constant_width = value.getBitWidth();
1282
1283 if (qual_type->isIntegralOrEnumerationType()) {
1284 if (type_width >= constant_width) {
1286 decl, value.toAPSInt().extOrTrunc(type_width));
1287 } else {
1289 "Class '{0}' has a member '{1}' of type '{2}' ({3} bits) "
1290 "which resolves to a wider constant value ({4} bits). "
1291 "Ignoring constant.",
1292 record_type.GetTypeName(), member_name,
1293 member_comp_type.GetTypeName(), type_width,
1294 constant_width);
1295 }
1296 } else {
1297 switch (member_comp_type.GetBasicTypeEnumeration()) {
1301 if (type_width == constant_width) {
1303 decl, value.toAPFloat());
1304 decl->setConstexpr(true);
1305 } else {
1307 "Class '{0}' has a member '{1}' of type '{2}' ({3} "
1308 "bits) which resolves to a constant value of mismatched "
1309 "width ({4} bits). Ignoring constant.",
1310 record_type.GetTypeName(), member_name,
1311 member_comp_type.GetTypeName(), type_width,
1312 constant_width);
1313 }
1314 break;
1315 default:
1316 break;
1317 }
1318 }
1319 }
1320
1321 m_uid_to_decl[member->getSymIndexId()] = decl;
1322
1323 break;
1324 }
1325 default:
1326 llvm_unreachable("unsupported PDB data kind");
1327 }
1328 }
1329}
1330
1332 lldb_private::SymbolFile &symbol_file,
1333 lldb_private::CompilerType &record_type, int record_kind,
1334 PDBBaseClassSymbolEnumerator &bases_enum,
1335 lldb_private::ClangASTImporter::LayoutInfo &layout_info) const {
1336 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> base_classes;
1337
1338 while (auto base = bases_enum.getNext()) {
1339 auto base_type = symbol_file.ResolveTypeUID(base->getTypeId());
1340 if (!base_type)
1341 continue;
1342
1343 auto base_comp_type = base_type->GetFullCompilerType();
1344 if (!base_comp_type.GetCompleteType()) {
1345 symbol_file.GetObjectFile()->GetModule()->ReportError(
1346 ":: Class '{0}' has a base class '{1}' "
1347 "which does not have a complete definition.",
1348 record_type.GetTypeName().GetCString(),
1349 base_comp_type.GetTypeName().GetCString());
1352 }
1353
1354 auto access = TranslateMemberAccess(base->getAccess());
1355
1356 auto is_virtual = base->isVirtualBaseClass();
1357
1358 std::unique_ptr<clang::CXXBaseSpecifier> base_spec =
1359 m_ast.CreateBaseClassSpecifier(
1360 base_comp_type.GetOpaqueQualType(), access, is_virtual,
1361 record_kind == llvm::to_underlying(clang::TagTypeKind::Class));
1362 lldbassert(base_spec);
1363
1364 base_classes.push_back(std::move(base_spec));
1365
1366 if (is_virtual)
1367 continue;
1368
1369 auto decl = m_ast.GetAsCXXRecordDecl(base_comp_type.GetOpaqueQualType());
1370 if (!decl)
1371 continue;
1372
1373 auto offset = clang::CharUnits::fromQuantity(base->getOffset());
1374 layout_info.base_offsets.insert(std::make_pair(decl, offset));
1375 }
1376
1377 m_ast.TransferBaseClasses(record_type.GetOpaqueQualType(),
1378 std::move(base_classes));
1379}
1380
1382 lldb_private::CompilerType &record_type,
1383 PDBFuncSymbolEnumerator &methods_enum) {
1384 while (std::unique_ptr<PDBSymbolFunc> method = methods_enum.getNext())
1385 if (clang::CXXMethodDecl *decl =
1386 AddRecordMethod(symbol_file, record_type, *method))
1387 m_uid_to_decl[method->getSymIndexId()] = decl;
1388}
1389
1390clang::CXXMethodDecl *
1392 lldb_private::CompilerType &record_type,
1393 const llvm::pdb::PDBSymbolFunc &method) const {
1394 std::string name =
1395 std::string(MSVCUndecoratedNameParser::DropScope(method.getName()));
1396
1397 Type *method_type = symbol_file.ResolveTypeUID(method.getSymIndexId());
1398 // MSVC specific __vecDelDtor.
1399 if (!method_type)
1400 return nullptr;
1401
1402 CompilerType method_comp_type = method_type->GetFullCompilerType();
1403 if (!method_comp_type.GetCompleteType()) {
1404 symbol_file.GetObjectFile()->GetModule()->ReportError(
1405 ":: Class '{0}' has a method '{1}' whose type cannot be completed.",
1406 record_type.GetTypeName().GetCString(),
1407 method_comp_type.GetTypeName().GetCString());
1410 }
1411
1412 // TODO: get mangled name for the method.
1413 return m_ast.AddMethodToCXXRecordType(
1414 record_type.GetOpaqueQualType(), name.c_str(),
1415 /*asm_label=*/{}, method_comp_type, method.isVirtual(), method.isStatic(),
1416 method.hasInlineAttribute(),
1417 /*is_explicit*/ false, // FIXME: Need this field in CodeView.
1418 /*is_attr_used*/ false,
1419 /*is_artificial*/ method.isCompilerGenerated());
1420}
#define lldbassert(x)
Definition LLDBAssert.h:16
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
static clang::CallingConv TranslateCallingConvention(PDB_CallingConv pdb_cc)
static int TranslateUdtKind(PDB_UdtType pdb_kind)
static clang::NamedDecl * GetDeclFromContextByName(const clang::ASTContext &ast, const clang::DeclContext &decl_context, llvm::StringRef name)
static ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin &pdb_type, CompilerType &compiler_type)
static std::unique_ptr< llvm::pdb::PDBSymbol > GetClassOrFunctionParent(const llvm::pdb::PDBSymbol &symbol)
static lldb::Encoding TranslateEnumEncoding(PDB_VariantType type)
static AccessType TranslateMemberAccess(PDB_MemberAccess access)
static CompilerType GetBuiltinTypeForPDBEncodingAndBitSize(TypeSystemClang &clang_ast, const PDBSymbolTypeBuiltin &pdb_type, Encoding encoding, uint32_t width)
static lldb::Encoding TranslateBuiltinEncoding(PDB_BuiltinType type)
static bool AddSourceInfoToDecl(const PDBSymbol &symbol, Declaration &decl)
static clang::MSInheritanceAttr::Spelling GetMSInheritance(const PDBSymbolTypeUDT &udt)
static bool IsAnonymousNamespaceName(llvm::StringRef name)
static std::optional< clang::CallingConv > TranslateCallingConvention(llvm::codeview::CallingConvention conv)
static bool IsAnonymousNamespaceName(llvm::StringRef name)
static clang::TagTypeKind TranslateUdtKind(const TagRecord &cr)
llvm::ArrayRef< MSVCUndecoratedNameSpecifier > GetSpecifiers() const
static llvm::StringRef DropScope(llvm::StringRef name)
lldb_private::TypeSystemClang & m_ast
clang::NamespaceDecl * FindNamespaceDecl(const clang::DeclContext *parent, llvm::StringRef name)
bool CompleteTypeFromUDT(lldb_private::SymbolFile &symbol_file, lldb_private::CompilerType &compiler_type, llvm::pdb::PDBSymbolTypeUDT &udt)
void AddRecordMethods(lldb_private::SymbolFile &symbol_file, lldb_private::CompilerType &record_type, PDBFuncSymbolEnumerator &methods_enum)
NamespacesSet m_namespaces
lldb::TypeSP CreateLLDBTypeFromPDBType(const llvm::pdb::PDBSymbol &type)
DeclContextToUidMap m_decl_context_to_uid
clang::DeclContext * GetDeclContextForSymbol(const llvm::pdb::PDBSymbol &symbol)
lldb_private::ClangASTImporter & GetClangASTImporter()
void ParseDeclsForDeclContext(const clang::DeclContext *decl_context)
llvm::pdb::ConcreteSymbolEnumerator< llvm::pdb::PDBSymbolData > PDBDataSymbolEnumerator
void AddRecordBases(lldb_private::SymbolFile &symbol_file, lldb_private::CompilerType &record_type, int record_kind, PDBBaseClassSymbolEnumerator &bases_enum, lldb_private::ClangASTImporter::LayoutInfo &layout_info) const
void AddRecordMembers(lldb_private::SymbolFile &symbol_file, lldb_private::CompilerType &record_type, PDBDataSymbolEnumerator &members_enum, lldb_private::ClangASTImporter::LayoutInfo &layout_info)
std::set< clang::NamespaceDecl * > NamespacesSet
UidToDeclMap m_uid_to_decl
bool CompleteTypeFromPDB(lldb_private::CompilerType &compiler_type)
bool AddEnumValue(lldb_private::CompilerType enum_type, const llvm::pdb::PDBSymbolData &data)
llvm::pdb::ConcreteSymbolEnumerator< llvm::pdb::PDBSymbolTypeBaseClass > PDBBaseClassSymbolEnumerator
PDBASTParser(lldb_private::TypeSystemClang &ast)
llvm::pdb::ConcreteSymbolEnumerator< llvm::pdb::PDBSymbolFunc > PDBFuncSymbolEnumerator
ParentToNamespacesMap m_parent_to_namespaces
clang::Decl * GetDeclForSymbol(const llvm::pdb::PDBSymbol &symbol)
clang::DeclContext * GetDeclContextContainingSymbol(const llvm::pdb::PDBSymbol &symbol)
clang::CXXMethodDecl * AddRecordMethod(lldb_private::SymbolFile &symbol_file, lldb_private::CompilerType &record_type, const llvm::pdb::PDBSymbolFunc &method) const
CXXRecordDeclToUidMap m_forward_decl_to_uid
llvm::pdb::IPDBSession & GetPDBSession()
lldb_private::Type * ResolveTypeUID(lldb::user_id_t type_uid) override
bool CompleteType(const CompilerType &compiler_type)
void SetRecordLayout(clang::RecordDecl *decl, const LayoutInfo &layout)
Sets the layout for the given RecordDecl.
void SetUserID(lldb::user_id_t user_id)
void SetIsDynamicCXXType(std::optional< bool > b)
Generic representation of a type in a programming language.
CompilerType AddConstModifier() const
Return a new CompilerType adds a const modifier to this type if this type is valid and the type syste...
CompilerType GetRValueReferenceType() const
Return a new CompilerType that is a R value reference to this type if this type is valid and the type...
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
lldb::opaque_compiler_type_t GetOpaqueQualType() const
CompilerType AddVolatileModifier() const
Return a new CompilerType adds a volatile modifier to this type if this type is valid and the type sy...
CompilerType AddRestrictModifier() const
Return a new CompilerType adds a restrict modifier to this type if this type is valid and the type sy...
CompilerType GetLValueReferenceType() const
Return a new CompilerType that is a L value reference to this type if this type is valid and the type...
ConstString GetTypeName(bool BaseOnly=false) const
bool GetCompleteType() const
Type Completion.
CompilerType CreateTypedef(const char *name, const CompilerDeclContext &decl_ctx, uint32_t payload) const
Create a typedef to this type using "name" as the name of the typedef this type is valid and the type...
A uniqued constant string class.
Definition ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
A class that describes the declaration location of a lldb object.
Definition Declaration.h:24
void SetLine(uint32_t line)
Set accessor for the declaration line number.
void SetColumn(uint16_t column)
Set accessor for the declaration column number.
void SetFile(const FileSpec &file_spec)
Set accessor for the declaration file specification.
A file utility class.
Definition FileSpec.h:57
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Provides public interface for all SymbolFiles.
Definition SymbolFile.h:51
virtual Type * ResolveTypeUID(lldb::user_id_t type_uid)=0
virtual ObjectFile * GetObjectFile()=0
A TypeSystem implementation based on Clang.
static clang::TypedefNameDecl * GetAsTypedefDecl(const CompilerType &type)
CompilerType GetBasicType(lldb::BasicType type)
CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding, size_t bit_size) override
static bool IsCXXClassType(const CompilerType &type)
static void SetFloatingInitializerForVariable(clang::VarDecl *var, const llvm::APFloat &init_value)
Initializes a variable with a floating point value.
static void SetIntegerInitializerForVariable(clang::VarDecl *var, const llvm::APInt &init_value)
Initializes a variable with an integer value.
static clang::VarDecl * AddVariableToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &var_type)
static void BuildIndirectFields(const CompilerType &type)
static bool CompleteTagDeclarationDefinition(const CompilerType &type)
static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type, bool has_extern)
static bool StartTagDeclarationDefinition(const CompilerType &type)
static clang::FieldDecl * AddFieldToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &field_type, uint32_t bitfield_bit_size)
static clang::EnumDecl * GetAsEnumDecl(const CompilerType &type)
clang::ASTContext & getASTContext() const
Returns the clang::ASTContext instance managed by this TypeSystemClang.
static CompilerType CreateMemberPointerType(const CompilerType &type, const CompilerType &pointee_type)
CompilerType GetForwardCompilerType()
Definition Type.cpp:782
@ eEncodingIsTypedefUID
This type is alias to a type whose UID is m_encoding_uid.
Definition Type.h:434
@ eEncodingIsUID
This type is the type whose UID is m_encoding_uid.
Definition Type.h:423
CompilerType GetLayoutCompilerType()
Definition Type.cpp:777
CompilerType GetFullCompilerType()
Definition Type.cpp:772
#define LLDB_INVALID_UID
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
@ eBasicTypeLongDouble
@ eLanguageTypeC_plus_plus
ISO C++:1998.
std::shared_ptr< lldb_private::Type > TypeSP
Encoding
Register encoding definitions.
@ eEncodingIEEE754
float
@ eEncodingUint
unsigned integer
@ eEncodingSint
signed integer
llvm::DenseMap< const clang::FieldDecl *, uint64_t > field_offsets
static clang::QualType GetQualType(const CompilerType &ct)
Definition ClangUtil.cpp:36
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition UserID.h:47