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