1#include "PdbAstBuilder.h"
2
3#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
4#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
5#include "llvm/DebugInfo/CodeView/RecordName.h"
6#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
7#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
8#include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
9#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
10#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
11#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
12#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
13#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
14#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
15#include "llvm/Demangle/MicrosoftDemangle.h"
16
17#include "Plugins/ExpressionParser/Clang/ClangASTMetadata.h"
18#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
19#include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
20#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
21#include "lldb/Core/Module.h"
22#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Utility/LLDBAssert.h"
24#include "PdbUtil.h"
25#include "UdtRecordCompleter.h"
26#include "SymbolFileNativePDB.h"
27#include <optional>
28
29using namespace lldb_private;
30using namespace lldb_private::npdb;
31using namespace llvm::codeview;
32using namespace llvm::pdb;
33
34namespace {
35struct CreateMethodDecl : public TypeVisitorCallbacks {
36  CreateMethodDecl(PdbIndex &m_index, TypeSystemClang &m_clang,
37                   TypeIndex func_type_index,
38                   clang::FunctionDecl *&function_decl,
39                   lldb::opaque_compiler_type_t parent_ty,
40                   llvm::StringRef proc_name, CompilerType func_ct)
41      : m_index(m_index), m_clang(m_clang), func_type_index(func_type_index),
42        function_decl(function_decl), parent_ty(parent_ty),
43        proc_name(proc_name), func_ct(func_ct) {}
44  PdbIndex &m_index;
45  TypeSystemClang &m_clang;
46  TypeIndex func_type_index;
47  clang::FunctionDecl *&function_decl;
48  lldb::opaque_compiler_type_t parent_ty;
49  llvm::StringRef proc_name;
50  CompilerType func_ct;
51
52  llvm::Error visitKnownMember(CVMemberRecord &cvr,
53                               OverloadedMethodRecord &overloaded) override {
54    TypeIndex method_list_idx = overloaded.MethodList;
55
56    CVType method_list_type = m_index.tpi().getType(method_list_idx);
57    assert(method_list_type.kind() == LF_METHODLIST);
58
59    MethodOverloadListRecord method_list;
60    llvm::cantFail(TypeDeserializer::deserializeAs<MethodOverloadListRecord>(
61        method_list_type, method_list));
62
63    for (const OneMethodRecord &method : method_list.Methods) {
64      if (method.getType().getIndex() == func_type_index.getIndex())
65        AddMethod(overloaded.Name, method.getAccess(), method.getOptions(),
66                  method.Attrs);
67    }
68
69    return llvm::Error::success();
70  }
71
72  llvm::Error visitKnownMember(CVMemberRecord &cvr,
73                               OneMethodRecord &record) override {
74    AddMethod(record.getName(), record.getAccess(), record.getOptions(),
75              record.Attrs);
76    return llvm::Error::success();
77  }
78
79  void AddMethod(llvm::StringRef name, MemberAccess access,
80                 MethodOptions options, MemberAttributes attrs) {
81    if (name != proc_name || function_decl)
82      return;
83    lldb::AccessType access_type = TranslateMemberAccess(access);
84    bool is_virtual = attrs.isVirtual();
85    bool is_static = attrs.isStatic();
86    bool is_artificial = (options & MethodOptions::CompilerGenerated) ==
87                         MethodOptions::CompilerGenerated;
88    function_decl = m_clang.AddMethodToCXXRecordType(
89        parent_ty, proc_name,
90        /*mangled_name=*/nullptr, func_ct, /*access=*/access_type,
91        /*is_virtual=*/is_virtual, /*is_static=*/is_static,
92        /*is_inline=*/false, /*is_explicit=*/false,
93        /*is_attr_used=*/false, /*is_artificial=*/is_artificial);
94  }
95};
96} // namespace
97
98static clang::TagTypeKind TranslateUdtKind(const TagRecord &cr) {
99  switch (cr.Kind) {
100  case TypeRecordKind::Class:
101    return clang::TTK_Class;
102  case TypeRecordKind::Struct:
103    return clang::TTK_Struct;
104  case TypeRecordKind::Union:
105    return clang::TTK_Union;
106  case TypeRecordKind::Interface:
107    return clang::TTK_Interface;
108  case TypeRecordKind::Enum:
109    return clang::TTK_Enum;
110  default:
111    lldbassert(false && "Invalid tag record kind!");
112    return clang::TTK_Struct;
113  }
114}
115
116static bool IsCVarArgsFunction(llvm::ArrayRef<TypeIndex> args) {
117  if (args.empty())
118    return false;
119  return args.back() == TypeIndex::None();
120}
121
122static bool
123AnyScopesHaveTemplateParams(llvm::ArrayRef<llvm::ms_demangle::Node *> scopes) {
124  for (llvm::ms_demangle::Node *n : scopes) {
125    auto *idn = static_cast<llvm::ms_demangle::IdentifierNode *>(n);
126    if (idn->TemplateParams)
127      return true;
128  }
129  return false;
130}
131
132static std::optional<clang::CallingConv>
133TranslateCallingConvention(llvm::codeview::CallingConvention conv) {
134  using CC = llvm::codeview::CallingConvention;
135  switch (conv) {
136
137  case CC::NearC:
138  case CC::FarC:
139    return clang::CallingConv::CC_C;
140  case CC::NearPascal:
141  case CC::FarPascal:
142    return clang::CallingConv::CC_X86Pascal;
143  case CC::NearFast:
144  case CC::FarFast:
145    return clang::CallingConv::CC_X86FastCall;
146  case CC::NearStdCall:
147  case CC::FarStdCall:
148    return clang::CallingConv::CC_X86StdCall;
149  case CC::ThisCall:
150    return clang::CallingConv::CC_X86ThisCall;
151  case CC::NearVector:
152    return clang::CallingConv::CC_X86VectorCall;
153  default:
154    return std::nullopt;
155  }
156}
157
158static bool IsAnonymousNamespaceName(llvm::StringRef name) {
159  return name == "`anonymous namespace'" || name == "`anonymous-namespace'";
160}
161
162PdbAstBuilder::PdbAstBuilder(TypeSystemClang &clang) : m_clang(clang) {}
163
164lldb_private::CompilerDeclContext PdbAstBuilder::GetTranslationUnitDecl() {
165  return ToCompilerDeclContext(*m_clang.GetTranslationUnitDecl());
166}
167
168std::pair<clang::DeclContext *, std::string>
169PdbAstBuilder::CreateDeclInfoForType(const TagRecord &record, TypeIndex ti) {
170  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
171      m_clang.GetSymbolFile()->GetBackingSymbolFile());
172  // FIXME: Move this to GetDeclContextContainingUID.
173  if (!record.hasUniqueName())
174    return CreateDeclInfoForUndecoratedName(record.Name);
175
176  llvm::ms_demangle::Demangler demangler;
177  StringView sv(record.UniqueName.begin(), record.UniqueName.size());
178  llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
179  if (demangler.Error)
180    return {m_clang.GetTranslationUnitDecl(), std::string(record.UniqueName)};
181
182  llvm::ms_demangle::IdentifierNode *idn =
183      ttn->QualifiedName->getUnqualifiedIdentifier();
184  std::string uname = idn->toString(llvm::ms_demangle::OF_NoTagSpecifier);
185
186  llvm::ms_demangle::NodeArrayNode *name_components =
187      ttn->QualifiedName->Components;
188  llvm::ArrayRef<llvm::ms_demangle::Node *> scopes(name_components->Nodes,
189                                                   name_components->Count - 1);
190
191  clang::DeclContext *context = m_clang.GetTranslationUnitDecl();
192
193  // If this type doesn't have a parent type in the debug info, then the best we
194  // can do is to say that it's either a series of namespaces (if the scope is
195  // non-empty), or the translation unit (if the scope is empty).
196  std::optional<TypeIndex> parent_index = pdb->GetParentType(ti);
197  if (!parent_index) {
198    if (scopes.empty())
199      return {context, uname};
200
201    // If there is no parent in the debug info, but some of the scopes have
202    // template params, then this is a case of bad debug info.  See, for
203    // example, llvm.org/pr39607.  We don't want to create an ambiguity between
204    // a NamespaceDecl and a CXXRecordDecl, so instead we create a class at
205    // global scope with the fully qualified name.
206    if (AnyScopesHaveTemplateParams(scopes))
207      return {context, std::string(record.Name)};
208
209    for (llvm::ms_demangle::Node *scope : scopes) {
210      auto *nii = static_cast<llvm::ms_demangle::NamedIdentifierNode *>(scope);
211      std::string str = nii->toString();
212      context = GetOrCreateNamespaceDecl(str.c_str(), *context);
213    }
214    return {context, uname};
215  }
216
217  // Otherwise, all we need to do is get the parent type of this type and
218  // recurse into our lazy type creation / AST reconstruction logic to get an
219  // LLDB TypeSP for the parent.  This will cause the AST to automatically get
220  // the right DeclContext created for any parent.
221  clang::QualType parent_qt = GetOrCreateType(*parent_index);
222  if (parent_qt.isNull())
223    return {nullptr, ""};
224
225  context = clang::TagDecl::castToDeclContext(parent_qt->getAsTagDecl());
226  return {context, uname};
227}
228
229static bool isLocalVariableType(SymbolKind K) {
230  switch (K) {
231  case S_REGISTER:
232  case S_REGREL32:
233  case S_LOCAL:
234    return true;
235  default:
236    break;
237  }
238  return false;
239}
240
241clang::Decl *PdbAstBuilder::GetOrCreateSymbolForId(PdbCompilandSymId id) {
242  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
243      m_clang.GetSymbolFile()->GetBackingSymbolFile());
244  PdbIndex &index = pdb->GetIndex();
245  CVSymbol cvs = index.ReadSymbolRecord(id);
246
247  if (isLocalVariableType(cvs.kind())) {
248    clang::DeclContext *scope = GetParentDeclContext(id);
249    if (!scope)
250      return nullptr;
251    clang::Decl *scope_decl = clang::Decl::castFromDeclContext(scope);
252    PdbCompilandSymId scope_id =
253        PdbSymUid(m_decl_to_status[scope_decl].uid).asCompilandSym();
254    return GetOrCreateVariableDecl(scope_id, id);
255  }
256
257  switch (cvs.kind()) {
258  case S_GPROC32:
259  case S_LPROC32:
260    return GetOrCreateFunctionDecl(id);
261  case S_GDATA32:
262  case S_LDATA32:
263  case S_GTHREAD32:
264  case S_CONSTANT:
265    // global variable
266    return nullptr;
267  case S_BLOCK32:
268    return GetOrCreateBlockDecl(id);
269  case S_INLINESITE:
270    return GetOrCreateInlinedFunctionDecl(id);
271  default:
272    return nullptr;
273  }
274}
275
276std::optional<CompilerDecl>
277PdbAstBuilder::GetOrCreateDeclForUid(PdbSymUid uid) {
278  if (clang::Decl *result = TryGetDecl(uid))
279    return ToCompilerDecl(*result);
280
281  clang::Decl *result = nullptr;
282  switch (uid.kind()) {
283  case PdbSymUidKind::CompilandSym:
284    result = GetOrCreateSymbolForId(uid.asCompilandSym());
285    break;
286  case PdbSymUidKind::Type: {
287    clang::QualType qt = GetOrCreateType(uid.asTypeSym());
288    if (qt.isNull())
289      return std::nullopt;
290    if (auto *tag = qt->getAsTagDecl()) {
291      result = tag;
292      break;
293    }
294    return std::nullopt;
295  }
296  default:
297    return std::nullopt;
298  }
299
300  if (!result)
301    return std::nullopt;
302  m_uid_to_decl[toOpaqueUid(uid)] = result;
303  return ToCompilerDecl(*result);
304}
305
306clang::DeclContext *PdbAstBuilder::GetOrCreateDeclContextForUid(PdbSymUid uid) {
307  if (uid.kind() == PdbSymUidKind::CompilandSym) {
308    if (uid.asCompilandSym().offset == 0)
309      return FromCompilerDeclContext(GetTranslationUnitDecl());
310  }
311  auto option = GetOrCreateDeclForUid(uid);
312  if (!option)
313    return nullptr;
314  clang::Decl *decl = FromCompilerDecl(*option);
315  if (!decl)
316    return nullptr;
317
318  return clang::Decl::castToDeclContext(decl);
319}
320
321std::pair<clang::DeclContext *, std::string>
322PdbAstBuilder::CreateDeclInfoForUndecoratedName(llvm::StringRef name) {
323  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
324      m_clang.GetSymbolFile()->GetBackingSymbolFile());
325  PdbIndex &index = pdb->GetIndex();
326  MSVCUndecoratedNameParser parser(name);
327  llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers();
328
329  auto *context = FromCompilerDeclContext(GetTranslationUnitDecl());
330
331  llvm::StringRef uname = specs.back().GetBaseName();
332  specs = specs.drop_back();
333  if (specs.empty())
334    return {context, std::string(name)};
335
336  llvm::StringRef scope_name = specs.back().GetFullName();
337
338  // It might be a class name, try that first.
339  std::vector<TypeIndex> types = index.tpi().findRecordsByName(scope_name);
340  while (!types.empty()) {
341    clang::QualType qt = GetOrCreateType(types.back());
342    if (qt.isNull())
343      continue;
344    clang::TagDecl *tag = qt->getAsTagDecl();
345    if (tag)
346      return {clang::TagDecl::castToDeclContext(tag), std::string(uname)};
347    types.pop_back();
348  }
349
350  // If that fails, treat it as a series of namespaces.
351  for (const MSVCUndecoratedNameSpecifier &spec : specs) {
352    std::string ns_name = spec.GetBaseName().str();
353    context = GetOrCreateNamespaceDecl(ns_name.c_str(), *context);
354  }
355  return {context, std::string(uname)};
356}
357
358clang::DeclContext *PdbAstBuilder::GetParentDeclContext(PdbSymUid uid) {
359  // We must do this *without* calling GetOrCreate on the current uid, as
360  // that would be an infinite recursion.
361  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
362      m_clang.GetSymbolFile()->GetBackingSymbolFile());
363  PdbIndex& index = pdb->GetIndex();
364  switch (uid.kind()) {
365  case PdbSymUidKind::CompilandSym: {
366    std::optional<PdbCompilandSymId> scope =
367        pdb->FindSymbolScope(uid.asCompilandSym());
368    if (scope)
369      return GetOrCreateDeclContextForUid(*scope);
370
371    CVSymbol sym = index.ReadSymbolRecord(uid.asCompilandSym());
372    return CreateDeclInfoForUndecoratedName(getSymbolName(sym)).first;
373  }
374  case PdbSymUidKind::Type: {
375    // It could be a namespace, class, or global.  We don't support nested
376    // functions yet.  Anyway, we just need to consult the parent type map.
377    PdbTypeSymId type_id = uid.asTypeSym();
378    std::optional<TypeIndex> parent_index = pdb->GetParentType(type_id.index);
379    if (!parent_index)
380      return FromCompilerDeclContext(GetTranslationUnitDecl());
381    return GetOrCreateDeclContextForUid(PdbTypeSymId(*parent_index));
382  }
383  case PdbSymUidKind::FieldListMember:
384    // In this case the parent DeclContext is the one for the class that this
385    // member is inside of.
386    break;
387  case PdbSymUidKind::GlobalSym: {
388    // If this refers to a compiland symbol, just recurse in with that symbol.
389    // The only other possibilities are S_CONSTANT and S_UDT, in which case we
390    // need to parse the undecorated name to figure out the scope, then look
391    // that up in the TPI stream.  If it's found, it's a type, othewrise it's
392    // a series of namespaces.
393    // FIXME: do this.
394    CVSymbol global = index.ReadSymbolRecord(uid.asGlobalSym());
395    switch (global.kind()) {
396    case SymbolKind::S_GDATA32:
397    case SymbolKind::S_LDATA32:
398      return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first;;
399    case SymbolKind::S_PROCREF:
400    case SymbolKind::S_LPROCREF: {
401      ProcRefSym ref{global.kind()};
402      llvm::cantFail(
403          SymbolDeserializer::deserializeAs<ProcRefSym>(global, ref));
404      PdbCompilandSymId cu_sym_id{ref.modi(), ref.SymOffset};
405      return GetParentDeclContext(cu_sym_id);
406    }
407    case SymbolKind::S_CONSTANT:
408    case SymbolKind::S_UDT:
409      return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first;
410    default:
411      break;
412    }
413    break;
414  }
415  default:
416    break;
417  }
418  return FromCompilerDeclContext(GetTranslationUnitDecl());
419}
420
421bool PdbAstBuilder::CompleteType(clang::QualType qt) {
422  if (qt.isNull())
423    return false;
424  clang::TagDecl *tag = qt->getAsTagDecl();
425  if (qt->isArrayType()) {
426    const clang::Type *element_type = qt->getArrayElementTypeNoTypeQual();
427    tag = element_type->getAsTagDecl();
428  }
429  if (!tag)
430    return false;
431
432  return CompleteTagDecl(*tag);
433}
434
435bool PdbAstBuilder::CompleteTagDecl(clang::TagDecl &tag) {
436  // If this is not in our map, it's an error.
437  auto status_iter = m_decl_to_status.find(&tag);
438  lldbassert(status_iter != m_decl_to_status.end());
439
440  // If it's already complete, just return.
441  DeclStatus &status = status_iter->second;
442  if (status.resolved)
443    return true;
444
445  PdbTypeSymId type_id = PdbSymUid(status.uid).asTypeSym();
446  PdbIndex &index = static_cast<SymbolFileNativePDB *>(
447                        m_clang.GetSymbolFile()->GetBackingSymbolFile())
448                        ->GetIndex();
449  lldbassert(IsTagRecord(type_id, index.tpi()));
450
451  clang::QualType tag_qt = m_clang.getASTContext().getTypeDeclType(&tag);
452  TypeSystemClang::SetHasExternalStorage(tag_qt.getAsOpaquePtr(), false);
453
454  TypeIndex tag_ti = type_id.index;
455  CVType cvt = index.tpi().getType(tag_ti);
456  if (cvt.kind() == LF_MODIFIER)
457    tag_ti = LookThroughModifierRecord(cvt);
458
459  PdbTypeSymId best_ti = GetBestPossibleDecl(tag_ti, index.tpi());
460  cvt = index.tpi().getType(best_ti.index);
461  lldbassert(IsTagRecord(cvt));
462
463  if (IsForwardRefUdt(cvt)) {
464    // If we can't find a full decl for this forward ref anywhere in the debug
465    // info, then we have no way to complete it.
466    return false;
467  }
468
469  TypeIndex field_list_ti = GetFieldListIndex(cvt);
470  CVType field_list_cvt = index.tpi().getType(field_list_ti);
471  if (field_list_cvt.kind() != LF_FIELDLIST)
472    return false;
473  FieldListRecord field_list;
474  if (llvm::Error error = TypeDeserializer::deserializeAs<FieldListRecord>(
475          field_list_cvt, field_list))
476    llvm::consumeError(std::move(error));
477
478  // Visit all members of this class, then perform any finalization necessary
479  // to complete the class.
480  CompilerType ct = ToCompilerType(tag_qt);
481  UdtRecordCompleter completer(best_ti, ct, tag, *this, index, m_decl_to_status,
482                               m_cxx_record_map);
483  llvm::Error error =
484      llvm::codeview::visitMemberRecordStream(field_list.Data, completer);
485  completer.complete();
486
487  m_decl_to_status[&tag].resolved = true;
488  if (error) {
489    llvm::consumeError(std::move(error));
490    return false;
491  }
492  return true;
493}
494
495clang::QualType PdbAstBuilder::CreateSimpleType(TypeIndex ti) {
496  if (ti == TypeIndex::NullptrT())
497    return GetBasicType(lldb::eBasicTypeNullPtr);
498
499  if (ti.getSimpleMode() != SimpleTypeMode::Direct) {
500    clang::QualType direct_type = GetOrCreateType(ti.makeDirect());
501    if (direct_type.isNull())
502      return {};
503    return m_clang.getASTContext().getPointerType(direct_type);
504  }
505
506  if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated)
507    return {};
508
509  lldb::BasicType bt = GetCompilerTypeForSimpleKind(ti.getSimpleKind());
510  if (bt == lldb::eBasicTypeInvalid)
511    return {};
512
513  return GetBasicType(bt);
514}
515
516clang::QualType PdbAstBuilder::CreatePointerType(const PointerRecord &pointer) {
517  clang::QualType pointee_type = GetOrCreateType(pointer.ReferentType);
518
519  // This can happen for pointers to LF_VTSHAPE records, which we shouldn't
520  // create in the AST.
521  if (pointee_type.isNull())
522    return {};
523
524  if (pointer.isPointerToMember()) {
525    MemberPointerInfo mpi = pointer.getMemberInfo();
526    clang::QualType class_type = GetOrCreateType(mpi.ContainingType);
527    if (class_type.isNull())
528      return {};
529    if (clang::TagDecl *tag = class_type->getAsTagDecl()) {
530      clang::MSInheritanceAttr::Spelling spelling;
531      switch (mpi.Representation) {
532      case llvm::codeview::PointerToMemberRepresentation::SingleInheritanceData:
533      case llvm::codeview::PointerToMemberRepresentation::
534          SingleInheritanceFunction:
535        spelling =
536            clang::MSInheritanceAttr::Spelling::Keyword_single_inheritance;
537        break;
538      case llvm::codeview::PointerToMemberRepresentation::
539          MultipleInheritanceData:
540      case llvm::codeview::PointerToMemberRepresentation::
541          MultipleInheritanceFunction:
542        spelling =
543            clang::MSInheritanceAttr::Spelling::Keyword_multiple_inheritance;
544        break;
545      case llvm::codeview::PointerToMemberRepresentation::
546          VirtualInheritanceData:
547      case llvm::codeview::PointerToMemberRepresentation::
548          VirtualInheritanceFunction:
549        spelling =
550            clang::MSInheritanceAttr::Spelling::Keyword_virtual_inheritance;
551        break;
552      case llvm::codeview::PointerToMemberRepresentation::Unknown:
553        spelling =
554            clang::MSInheritanceAttr::Spelling::Keyword_unspecified_inheritance;
555        break;
556      default:
557        spelling = clang::MSInheritanceAttr::Spelling::SpellingNotCalculated;
558        break;
559      }
560      tag->addAttr(clang::MSInheritanceAttr::CreateImplicit(
561          m_clang.getASTContext(), spelling));
562    }
563    return m_clang.getASTContext().getMemberPointerType(
564        pointee_type, class_type.getTypePtr());
565  }
566
567  clang::QualType pointer_type;
568  if (pointer.getMode() == PointerMode::LValueReference)
569    pointer_type = m_clang.getASTContext().getLValueReferenceType(pointee_type);
570  else if (pointer.getMode() == PointerMode::RValueReference)
571    pointer_type = m_clang.getASTContext().getRValueReferenceType(pointee_type);
572  else
573    pointer_type = m_clang.getASTContext().getPointerType(pointee_type);
574
575  if ((pointer.getOptions() & PointerOptions::Const) != PointerOptions::None)
576    pointer_type.addConst();
577
578  if ((pointer.getOptions() & PointerOptions::Volatile) != PointerOptions::None)
579    pointer_type.addVolatile();
580
581  if ((pointer.getOptions() & PointerOptions::Restrict) != PointerOptions::None)
582    pointer_type.addRestrict();
583
584  return pointer_type;
585}
586
587clang::QualType
588PdbAstBuilder::CreateModifierType(const ModifierRecord &modifier) {
589  clang::QualType unmodified_type = GetOrCreateType(modifier.ModifiedType);
590  if (unmodified_type.isNull())
591    return {};
592
593  if ((modifier.Modifiers & ModifierOptions::Const) != ModifierOptions::None)
594    unmodified_type.addConst();
595  if ((modifier.Modifiers & ModifierOptions::Volatile) != ModifierOptions::None)
596    unmodified_type.addVolatile();
597
598  return unmodified_type;
599}
600
601clang::QualType PdbAstBuilder::CreateRecordType(PdbTypeSymId id,
602                                                const TagRecord &record) {
603  clang::DeclContext *context = nullptr;
604  std::string uname;
605  std::tie(context, uname) = CreateDeclInfoForType(record, id.index);
606  if (!context)
607    return {};
608
609  clang::TagTypeKind ttk = TranslateUdtKind(record);
610  lldb::AccessType access =
611      (ttk == clang::TTK_Class) ? lldb::eAccessPrivate : lldb::eAccessPublic;
612
613  ClangASTMetadata metadata;
614  metadata.SetUserID(toOpaqueUid(id));
615  metadata.SetIsDynamicCXXType(false);
616
617  CompilerType ct =
618      m_clang.CreateRecordType(context, OptionalClangModuleID(), access, uname,
619                               ttk, lldb::eLanguageTypeC_plus_plus, &metadata);
620
621  lldbassert(ct.IsValid());
622
623  TypeSystemClang::StartTagDeclarationDefinition(ct);
624
625  // Even if it's possible, don't complete it at this point. Just mark it
626  // forward resolved, and if/when LLDB needs the full definition, it can
627  // ask us.
628  clang::QualType result =
629      clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
630
631  TypeSystemClang::SetHasExternalStorage(result.getAsOpaquePtr(), true);
632  return result;
633}
634
635clang::Decl *PdbAstBuilder::TryGetDecl(PdbSymUid uid) const {
636  auto iter = m_uid_to_decl.find(toOpaqueUid(uid));
637  if (iter != m_uid_to_decl.end())
638    return iter->second;
639  return nullptr;
640}
641
642clang::NamespaceDecl *
643PdbAstBuilder::GetOrCreateNamespaceDecl(const char *name,
644                                        clang::DeclContext &context) {
645  return m_clang.GetUniqueNamespaceDeclaration(
646      IsAnonymousNamespaceName(name) ? nullptr : name, &context,
647      OptionalClangModuleID());
648}
649
650clang::BlockDecl *
651PdbAstBuilder::GetOrCreateBlockDecl(PdbCompilandSymId block_id) {
652  if (clang::Decl *decl = TryGetDecl(block_id))
653    return llvm::dyn_cast<clang::BlockDecl>(decl);
654
655  clang::DeclContext *scope = GetParentDeclContext(block_id);
656
657  clang::BlockDecl *block_decl =
658      m_clang.CreateBlockDeclaration(scope, OptionalClangModuleID());
659  m_uid_to_decl.insert({toOpaqueUid(block_id), block_decl});
660
661  DeclStatus status;
662  status.resolved = true;
663  status.uid = toOpaqueUid(block_id);
664  m_decl_to_status.insert({block_decl, status});
665
666  return block_decl;
667}
668
669clang::VarDecl *PdbAstBuilder::CreateVariableDecl(PdbSymUid uid, CVSymbol sym,
670                                                  clang::DeclContext &scope) {
671  VariableInfo var_info = GetVariableNameInfo(sym);
672  clang::QualType qt = GetOrCreateType(var_info.type);
673  if (qt.isNull())
674    return nullptr;
675
676  clang::VarDecl *var_decl = m_clang.CreateVariableDeclaration(
677      &scope, OptionalClangModuleID(), var_info.name.str().c_str(), qt);
678
679  m_uid_to_decl[toOpaqueUid(uid)] = var_decl;
680  DeclStatus status;
681  status.resolved = true;
682  status.uid = toOpaqueUid(uid);
683  m_decl_to_status.insert({var_decl, status});
684  return var_decl;
685}
686
687clang::VarDecl *
688PdbAstBuilder::GetOrCreateVariableDecl(PdbCompilandSymId scope_id,
689                                       PdbCompilandSymId var_id) {
690  if (clang::Decl *decl = TryGetDecl(var_id))
691    return llvm::dyn_cast<clang::VarDecl>(decl);
692
693  clang::DeclContext *scope = GetOrCreateDeclContextForUid(scope_id);
694  if (!scope)
695    return nullptr;
696
697  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
698      m_clang.GetSymbolFile()->GetBackingSymbolFile());
699  PdbIndex &index = pdb->GetIndex();
700  CVSymbol sym = index.ReadSymbolRecord(var_id);
701  return CreateVariableDecl(PdbSymUid(var_id), sym, *scope);
702}
703
704clang::VarDecl *PdbAstBuilder::GetOrCreateVariableDecl(PdbGlobalSymId var_id) {
705  if (clang::Decl *decl = TryGetDecl(var_id))
706    return llvm::dyn_cast<clang::VarDecl>(decl);
707
708  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
709      m_clang.GetSymbolFile()->GetBackingSymbolFile());
710  PdbIndex &index = pdb->GetIndex();
711  CVSymbol sym = index.ReadSymbolRecord(var_id);
712  auto context = FromCompilerDeclContext(GetTranslationUnitDecl());
713  return CreateVariableDecl(PdbSymUid(var_id), sym, *context);
714}
715
716clang::TypedefNameDecl *
717PdbAstBuilder::GetOrCreateTypedefDecl(PdbGlobalSymId id) {
718  if (clang::Decl *decl = TryGetDecl(id))
719    return llvm::dyn_cast<clang::TypedefNameDecl>(decl);
720
721  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
722      m_clang.GetSymbolFile()->GetBackingSymbolFile());
723  PdbIndex &index = pdb->GetIndex();
724  CVSymbol sym = index.ReadSymbolRecord(id);
725  lldbassert(sym.kind() == S_UDT);
726  UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
727
728  clang::DeclContext *scope = GetParentDeclContext(id);
729
730  PdbTypeSymId real_type_id{udt.Type, false};
731  clang::QualType qt = GetOrCreateType(real_type_id);
732  if (qt.isNull() || !scope)
733    return nullptr;
734
735  std::string uname = std::string(DropNameScope(udt.Name));
736
737  CompilerType ct = ToCompilerType(qt).CreateTypedef(
738      uname.c_str(), ToCompilerDeclContext(*scope), 0);
739  clang::TypedefNameDecl *tnd = m_clang.GetAsTypedefDecl(ct);
740  DeclStatus status;
741  status.resolved = true;
742  status.uid = toOpaqueUid(id);
743  m_decl_to_status.insert({tnd, status});
744  return tnd;
745}
746
747clang::QualType PdbAstBuilder::GetBasicType(lldb::BasicType type) {
748  CompilerType ct = m_clang.GetBasicType(type);
749  return clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
750}
751
752clang::QualType PdbAstBuilder::CreateType(PdbTypeSymId type) {
753  if (type.index.isSimple())
754    return CreateSimpleType(type.index);
755
756  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
757      m_clang.GetSymbolFile()->GetBackingSymbolFile());
758  PdbIndex &index = pdb->GetIndex();
759  CVType cvt = index.tpi().getType(type.index);
760
761  if (cvt.kind() == LF_MODIFIER) {
762    ModifierRecord modifier;
763    llvm::cantFail(
764        TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier));
765    return CreateModifierType(modifier);
766  }
767
768  if (cvt.kind() == LF_POINTER) {
769    PointerRecord pointer;
770    llvm::cantFail(
771        TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer));
772    return CreatePointerType(pointer);
773  }
774
775  if (IsTagRecord(cvt)) {
776    CVTagRecord tag = CVTagRecord::create(cvt);
777    if (tag.kind() == CVTagRecord::Union)
778      return CreateRecordType(type.index, tag.asUnion());
779    if (tag.kind() == CVTagRecord::Enum)
780      return CreateEnumType(type.index, tag.asEnum());
781    return CreateRecordType(type.index, tag.asClass());
782  }
783
784  if (cvt.kind() == LF_ARRAY) {
785    ArrayRecord ar;
786    llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar));
787    return CreateArrayType(ar);
788  }
789
790  if (cvt.kind() == LF_PROCEDURE) {
791    ProcedureRecord pr;
792    llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr));
793    return CreateFunctionType(pr.ArgumentList, pr.ReturnType, pr.CallConv);
794  }
795
796  if (cvt.kind() == LF_MFUNCTION) {
797    MemberFunctionRecord mfr;
798    llvm::cantFail(
799        TypeDeserializer::deserializeAs<MemberFunctionRecord>(cvt, mfr));
800    return CreateFunctionType(mfr.ArgumentList, mfr.ReturnType, mfr.CallConv);
801  }
802
803  return {};
804}
805
806clang::QualType PdbAstBuilder::GetOrCreateType(PdbTypeSymId type) {
807  if (type.index.isNoneType())
808    return {};
809
810  lldb::user_id_t uid = toOpaqueUid(type);
811  auto iter = m_uid_to_type.find(uid);
812  if (iter != m_uid_to_type.end())
813    return iter->second;
814
815  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
816      m_clang.GetSymbolFile()->GetBackingSymbolFile());
817  PdbIndex &index = pdb->GetIndex();
818  PdbTypeSymId best_type = GetBestPossibleDecl(type, index.tpi());
819
820  clang::QualType qt;
821  if (best_type.index != type.index) {
822    // This is a forward decl.  Call GetOrCreate on the full decl, then map the
823    // forward decl id to the full decl QualType.
824    clang::QualType qt = GetOrCreateType(best_type);
825    if (qt.isNull())
826      return {};
827    m_uid_to_type[toOpaqueUid(type)] = qt;
828    return qt;
829  }
830
831  // This is either a full decl, or a forward decl with no matching full decl
832  // in the debug info.
833  qt = CreateType(type);
834  if (qt.isNull())
835    return {};
836
837  m_uid_to_type[toOpaqueUid(type)] = qt;
838  if (IsTagRecord(type, index.tpi())) {
839    clang::TagDecl *tag = qt->getAsTagDecl();
840    lldbassert(m_decl_to_status.count(tag) == 0);
841
842    DeclStatus &status = m_decl_to_status[tag];
843    status.uid = uid;
844    status.resolved = false;
845  }
846  return qt;
847}
848
849clang::FunctionDecl *
850PdbAstBuilder::CreateFunctionDecl(PdbCompilandSymId func_id,
851                                  llvm::StringRef func_name, TypeIndex func_ti,
852                                  CompilerType func_ct, uint32_t param_count,
853                                  clang::StorageClass func_storage,
854                                  bool is_inline, clang::DeclContext *parent) {
855  clang::FunctionDecl *function_decl = nullptr;
856  if (parent->isRecord()) {
857    SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
858        m_clang.GetSymbolFile()->GetBackingSymbolFile());
859    PdbIndex &index = pdb->GetIndex();
860    clang::QualType parent_qt = llvm::cast<clang::TypeDecl>(parent)
861                                    ->getTypeForDecl()
862                                    ->getCanonicalTypeInternal();
863    lldb::opaque_compiler_type_t parent_opaque_ty =
864        ToCompilerType(parent_qt).GetOpaqueQualType();
865    // FIXME: Remove this workaround.
866    auto iter = m_cxx_record_map.find(parent_opaque_ty);
867    if (iter != m_cxx_record_map.end()) {
868      if (iter->getSecond().contains({func_name, func_ct})) {
869        return nullptr;
870      }
871    }
872
873    CVType cvt = index.tpi().getType(func_ti);
874    MemberFunctionRecord func_record(static_cast<TypeRecordKind>(cvt.kind()));
875    llvm::cantFail(TypeDeserializer::deserializeAs<MemberFunctionRecord>(
876        cvt, func_record));
877    TypeIndex class_index = func_record.getClassType();
878
879    CVType parent_cvt = index.tpi().getType(class_index);
880    TagRecord tag_record = CVTagRecord::create(parent_cvt).asTag();
881    // If it's a forward reference, try to get the real TypeIndex.
882    if (tag_record.isForwardRef()) {
883      llvm::Expected<TypeIndex> eti =
884          index.tpi().findFullDeclForForwardRef(class_index);
885      if (eti) {
886        tag_record = CVTagRecord::create(index.tpi().getType(*eti)).asTag();
887      }
888    }
889    if (!tag_record.FieldList.isSimple()) {
890      CVType field_list_cvt = index.tpi().getType(tag_record.FieldList);
891      FieldListRecord field_list;
892      if (llvm::Error error = TypeDeserializer::deserializeAs<FieldListRecord>(
893              field_list_cvt, field_list))
894        llvm::consumeError(std::move(error));
895      CreateMethodDecl process(index, m_clang, func_ti, function_decl,
896                               parent_opaque_ty, func_name, func_ct);
897      if (llvm::Error err = visitMemberRecordStream(field_list.Data, process))
898        llvm::consumeError(std::move(err));
899    }
900
901    if (!function_decl) {
902      function_decl = m_clang.AddMethodToCXXRecordType(
903          parent_opaque_ty, func_name,
904          /*mangled_name=*/nullptr, func_ct,
905          /*access=*/lldb::AccessType::eAccessPublic,
906          /*is_virtual=*/false, /*is_static=*/false,
907          /*is_inline=*/false, /*is_explicit=*/false,
908          /*is_attr_used=*/false, /*is_artificial=*/false);
909    }
910    m_cxx_record_map[parent_opaque_ty].insert({func_name, func_ct});
911  } else {
912    function_decl = m_clang.CreateFunctionDeclaration(
913        parent, OptionalClangModuleID(), func_name, func_ct, func_storage,
914        is_inline);
915    CreateFunctionParameters(func_id, *function_decl, param_count);
916  }
917  return function_decl;
918}
919
920clang::FunctionDecl *
921PdbAstBuilder::GetOrCreateInlinedFunctionDecl(PdbCompilandSymId inlinesite_id) {
922  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
923      m_clang.GetSymbolFile()->GetBackingSymbolFile());
924  PdbIndex &index = pdb->GetIndex();
925  CompilandIndexItem *cii =
926      index.compilands().GetCompiland(inlinesite_id.modi);
927  CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(inlinesite_id.offset);
928  InlineSiteSym inline_site(static_cast<SymbolRecordKind>(sym.kind()));
929  cantFail(SymbolDeserializer::deserializeAs<InlineSiteSym>(sym, inline_site));
930
931  // Inlinee is the id index to the function id record that is inlined.
932  PdbTypeSymId func_id(inline_site.Inlinee, true);
933  // Look up the function decl by the id index to see if we have created a
934  // function decl for a different inlinesite that refers the same function.
935  if (clang::Decl *decl = TryGetDecl(func_id))
936    return llvm::dyn_cast<clang::FunctionDecl>(decl);
937  clang::FunctionDecl *function_decl =
938      CreateFunctionDeclFromId(func_id, inlinesite_id);
939  if (function_decl == nullptr)
940    return nullptr;
941
942  // Use inline site id in m_decl_to_status because it's expected to be a
943  // PdbCompilandSymId so that we can parse local variables info after it.
944  uint64_t inlinesite_uid = toOpaqueUid(inlinesite_id);
945  DeclStatus status;
946  status.resolved = true;
947  status.uid = inlinesite_uid;
948  m_decl_to_status.insert({function_decl, status});
949  // Use the index in IPI stream as uid in m_uid_to_decl, because index in IPI
950  // stream are unique and there could be multiple inline sites (different ids)
951  // referring the same inline function. This avoid creating multiple same
952  // inline function delcs.
953  uint64_t func_uid = toOpaqueUid(func_id);
954  lldbassert(m_uid_to_decl.count(func_uid) == 0);
955  m_uid_to_decl[func_uid] = function_decl;
956  return function_decl;
957}
958
959clang::FunctionDecl *
960PdbAstBuilder::CreateFunctionDeclFromId(PdbTypeSymId func_tid,
961                                        PdbCompilandSymId func_sid) {
962  lldbassert(func_tid.is_ipi);
963  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
964      m_clang.GetSymbolFile()->GetBackingSymbolFile());
965  PdbIndex &index = pdb->GetIndex();
966  CVType func_cvt = index.ipi().getType(func_tid.index);
967  llvm::StringRef func_name;
968  TypeIndex func_ti;
969  clang::DeclContext *parent = nullptr;
970  switch (func_cvt.kind()) {
971  case LF_MFUNC_ID: {
972    MemberFuncIdRecord mfr;
973    cantFail(
974        TypeDeserializer::deserializeAs<MemberFuncIdRecord>(func_cvt, mfr));
975    func_name = mfr.getName();
976    func_ti = mfr.getFunctionType();
977    PdbTypeSymId class_type_id(mfr.ClassType, false);
978    parent = GetOrCreateDeclContextForUid(class_type_id);
979    break;
980  }
981  case LF_FUNC_ID: {
982    FuncIdRecord fir;
983    cantFail(TypeDeserializer::deserializeAs<FuncIdRecord>(func_cvt, fir));
984    func_name = fir.getName();
985    func_ti = fir.getFunctionType();
986    parent = FromCompilerDeclContext(GetTranslationUnitDecl());
987    if (!fir.ParentScope.isNoneType()) {
988      CVType parent_cvt = index.ipi().getType(fir.ParentScope);
989      if (parent_cvt.kind() == LF_STRING_ID) {
990        StringIdRecord sir;
991        cantFail(
992            TypeDeserializer::deserializeAs<StringIdRecord>(parent_cvt, sir));
993        parent = GetOrCreateNamespaceDecl(sir.String.data(), *parent);
994      }
995    }
996    break;
997  }
998  default:
999    lldbassert(false && "Invalid function id type!");
1000  }
1001  clang::QualType func_qt = GetOrCreateType(func_ti);
1002  if (func_qt.isNull() || !parent)
1003    return nullptr;
1004  CompilerType func_ct = ToCompilerType(func_qt);
1005  uint32_t param_count =
1006      llvm::cast<clang::FunctionProtoType>(func_qt)->getNumParams();
1007  return CreateFunctionDecl(func_sid, func_name, func_ti, func_ct, param_count,
1008                            clang::SC_None, true, parent);
1009}
1010
1011clang::FunctionDecl *
1012PdbAstBuilder::GetOrCreateFunctionDecl(PdbCompilandSymId func_id) {
1013  if (clang::Decl *decl = TryGetDecl(func_id))
1014    return llvm::dyn_cast<clang::FunctionDecl>(decl);
1015
1016  clang::DeclContext *parent = GetParentDeclContext(PdbSymUid(func_id));
1017  if (!parent)
1018    return nullptr;
1019  std::string context_name;
1020  if (clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(parent)) {
1021    context_name = ns->getQualifiedNameAsString();
1022  } else if (clang::TagDecl *tag = llvm::dyn_cast<clang::TagDecl>(parent)) {
1023    context_name = tag->getQualifiedNameAsString();
1024  }
1025
1026  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1027      m_clang.GetSymbolFile()->GetBackingSymbolFile());
1028  PdbIndex &index = pdb->GetIndex();
1029  CVSymbol cvs = index.ReadSymbolRecord(func_id);
1030  ProcSym proc(static_cast<SymbolRecordKind>(cvs.kind()));
1031  llvm::cantFail(SymbolDeserializer::deserializeAs<ProcSym>(cvs, proc));
1032
1033  PdbTypeSymId type_id(proc.FunctionType);
1034  clang::QualType qt = GetOrCreateType(type_id);
1035  if (qt.isNull())
1036    return nullptr;
1037
1038  clang::StorageClass storage = clang::SC_None;
1039  if (proc.Kind == SymbolRecordKind::ProcSym)
1040    storage = clang::SC_Static;
1041
1042  const clang::FunctionProtoType *func_type =
1043      llvm::dyn_cast<clang::FunctionProtoType>(qt);
1044
1045  CompilerType func_ct = ToCompilerType(qt);
1046
1047  llvm::StringRef proc_name = proc.Name;
1048  proc_name.consume_front(context_name);
1049  proc_name.consume_front("::");
1050  clang::FunctionDecl *function_decl =
1051      CreateFunctionDecl(func_id, proc_name, proc.FunctionType, func_ct,
1052                         func_type->getNumParams(), storage, false, parent);
1053  if (function_decl == nullptr)
1054    return nullptr;
1055
1056  lldbassert(m_uid_to_decl.count(toOpaqueUid(func_id)) == 0);
1057  m_uid_to_decl[toOpaqueUid(func_id)] = function_decl;
1058  DeclStatus status;
1059  status.resolved = true;
1060  status.uid = toOpaqueUid(func_id);
1061  m_decl_to_status.insert({function_decl, status});
1062
1063  return function_decl;
1064}
1065
1066void PdbAstBuilder::CreateFunctionParameters(PdbCompilandSymId func_id,
1067                                             clang::FunctionDecl &function_decl,
1068                                             uint32_t param_count) {
1069  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1070      m_clang.GetSymbolFile()->GetBackingSymbolFile());
1071  PdbIndex &index = pdb->GetIndex();
1072  CompilandIndexItem *cii = index.compilands().GetCompiland(func_id.modi);
1073  CVSymbolArray scope =
1074      cii->m_debug_stream.getSymbolArrayForScope(func_id.offset);
1075
1076  scope.drop_front();
1077  auto begin = scope.begin();
1078  auto end = scope.end();
1079  std::vector<clang::ParmVarDecl *> params;
1080  for (uint32_t i = 0; i < param_count && begin != end;) {
1081    uint32_t record_offset = begin.offset();
1082    CVSymbol sym = *begin++;
1083
1084    TypeIndex param_type;
1085    llvm::StringRef param_name;
1086    switch (sym.kind()) {
1087    case S_REGREL32: {
1088      RegRelativeSym reg(SymbolRecordKind::RegRelativeSym);
1089      cantFail(SymbolDeserializer::deserializeAs<RegRelativeSym>(sym, reg));
1090      param_type = reg.Type;
1091      param_name = reg.Name;
1092      break;
1093    }
1094    case S_REGISTER: {
1095      RegisterSym reg(SymbolRecordKind::RegisterSym);
1096      cantFail(SymbolDeserializer::deserializeAs<RegisterSym>(sym, reg));
1097      param_type = reg.Index;
1098      param_name = reg.Name;
1099      break;
1100    }
1101    case S_LOCAL: {
1102      LocalSym local(SymbolRecordKind::LocalSym);
1103      cantFail(SymbolDeserializer::deserializeAs<LocalSym>(sym, local));
1104      if ((local.Flags & LocalSymFlags::IsParameter) == LocalSymFlags::None)
1105        continue;
1106      param_type = local.Type;
1107      param_name = local.Name;
1108      break;
1109    }
1110    case S_BLOCK32:
1111    case S_INLINESITE:
1112    case S_INLINESITE2:
1113      // All parameters should come before the first block/inlinesite.  If that
1114      // isn't the case, then perhaps this is bad debug info that doesn't
1115      // contain information about all parameters.
1116      return;
1117    default:
1118      continue;
1119    }
1120
1121    PdbCompilandSymId param_uid(func_id.modi, record_offset);
1122    clang::QualType qt = GetOrCreateType(param_type);
1123    if (qt.isNull())
1124      return;
1125
1126    CompilerType param_type_ct = m_clang.GetType(qt);
1127    clang::ParmVarDecl *param = m_clang.CreateParameterDeclaration(
1128        &function_decl, OptionalClangModuleID(), param_name.str().c_str(),
1129        param_type_ct, clang::SC_None, true);
1130    lldbassert(m_uid_to_decl.count(toOpaqueUid(param_uid)) == 0);
1131
1132    m_uid_to_decl[toOpaqueUid(param_uid)] = param;
1133    params.push_back(param);
1134    ++i;
1135  }
1136
1137  if (!params.empty() && params.size() == param_count)
1138    m_clang.SetFunctionParameters(&function_decl, params);
1139}
1140
1141clang::QualType PdbAstBuilder::CreateEnumType(PdbTypeSymId id,
1142                                              const EnumRecord &er) {
1143  clang::DeclContext *decl_context = nullptr;
1144  std::string uname;
1145  std::tie(decl_context, uname) = CreateDeclInfoForType(er, id.index);
1146  if (!decl_context)
1147    return {};
1148
1149  clang::QualType underlying_type = GetOrCreateType(er.UnderlyingType);
1150  if (underlying_type.isNull())
1151    return {};
1152
1153  Declaration declaration;
1154  CompilerType enum_ct = m_clang.CreateEnumerationType(
1155      uname, decl_context, OptionalClangModuleID(), declaration,
1156      ToCompilerType(underlying_type), er.isScoped());
1157
1158  TypeSystemClang::StartTagDeclarationDefinition(enum_ct);
1159  TypeSystemClang::SetHasExternalStorage(enum_ct.GetOpaqueQualType(), true);
1160
1161  return clang::QualType::getFromOpaquePtr(enum_ct.GetOpaqueQualType());
1162}
1163
1164clang::QualType PdbAstBuilder::CreateArrayType(const ArrayRecord &ar) {
1165  clang::QualType element_type = GetOrCreateType(ar.ElementType);
1166
1167  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1168      m_clang.GetSymbolFile()->GetBackingSymbolFile());
1169  PdbIndex &index = pdb->GetIndex();
1170  uint64_t element_size = GetSizeOfType({ar.ElementType}, index.tpi());
1171  if (element_type.isNull() || element_size == 0)
1172    return {};
1173  uint64_t element_count = ar.Size / element_size;
1174
1175  CompilerType array_ct = m_clang.CreateArrayType(ToCompilerType(element_type),
1176                                                  element_count, false);
1177  return clang::QualType::getFromOpaquePtr(array_ct.GetOpaqueQualType());
1178}
1179
1180clang::QualType PdbAstBuilder::CreateFunctionType(
1181    TypeIndex args_type_idx, TypeIndex return_type_idx,
1182    llvm::codeview::CallingConvention calling_convention) {
1183  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1184      m_clang.GetSymbolFile()->GetBackingSymbolFile());
1185  PdbIndex &index = pdb->GetIndex();
1186  TpiStream &stream = index.tpi();
1187  CVType args_cvt = stream.getType(args_type_idx);
1188  ArgListRecord args;
1189  llvm::cantFail(
1190      TypeDeserializer::deserializeAs<ArgListRecord>(args_cvt, args));
1191
1192  llvm::ArrayRef<TypeIndex> arg_indices = llvm::ArrayRef(args.ArgIndices);
1193  bool is_variadic = IsCVarArgsFunction(arg_indices);
1194  if (is_variadic)
1195    arg_indices = arg_indices.drop_back();
1196
1197  std::vector<CompilerType> arg_types;
1198  arg_types.reserve(arg_indices.size());
1199
1200  for (TypeIndex arg_index : arg_indices) {
1201    clang::QualType arg_type = GetOrCreateType(arg_index);
1202    if (arg_type.isNull())
1203      continue;
1204    arg_types.push_back(ToCompilerType(arg_type));
1205  }
1206
1207  clang::QualType return_type = GetOrCreateType(return_type_idx);
1208  if (return_type.isNull())
1209    return {};
1210
1211  std::optional<clang::CallingConv> cc =
1212      TranslateCallingConvention(calling_convention);
1213  if (!cc)
1214    return {};
1215
1216  CompilerType return_ct = ToCompilerType(return_type);
1217  CompilerType func_sig_ast_type = m_clang.CreateFunctionType(
1218      return_ct, arg_types.data(), arg_types.size(), is_variadic, 0, *cc);
1219
1220  return clang::QualType::getFromOpaquePtr(
1221      func_sig_ast_type.GetOpaqueQualType());
1222}
1223
1224static bool isTagDecl(clang::DeclContext &context) {
1225  return llvm::isa<clang::TagDecl>(&context);
1226}
1227
1228static bool isFunctionDecl(clang::DeclContext &context) {
1229  return llvm::isa<clang::FunctionDecl>(&context);
1230}
1231
1232static bool isBlockDecl(clang::DeclContext &context) {
1233  return llvm::isa<clang::BlockDecl>(&context);
1234}
1235
1236void PdbAstBuilder::ParseNamespace(clang::DeclContext &context) {
1237  clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(&context);
1238  if (m_parsed_namespaces.contains(ns))
1239    return;
1240  std::string qname = ns->getQualifiedNameAsString();
1241  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1242      m_clang.GetSymbolFile()->GetBackingSymbolFile());
1243  PdbIndex &index = pdb->GetIndex();
1244  TypeIndex ti{index.tpi().TypeIndexBegin()};
1245  for (const CVType &cvt : index.tpi().typeArray()) {
1246    PdbTypeSymId tid{ti};
1247    ++ti;
1248
1249    if (!IsTagRecord(cvt))
1250      continue;
1251
1252    CVTagRecord tag = CVTagRecord::create(cvt);
1253
1254    // Call CreateDeclInfoForType unconditionally so that the namespace info
1255    // gets created.  But only call CreateRecordType if the namespace name
1256    // matches.
1257    clang::DeclContext *context = nullptr;
1258    std::string uname;
1259    std::tie(context, uname) = CreateDeclInfoForType(tag.asTag(), tid.index);
1260    if (!context || !context->isNamespace())
1261      continue;
1262
1263    clang::NamespaceDecl *ns = llvm::cast<clang::NamespaceDecl>(context);
1264    llvm::StringRef ns_name = ns->getName();
1265    if (ns_name.startswith(qname)) {
1266      ns_name = ns_name.drop_front(qname.size());
1267      if (ns_name.startswith("::"))
1268        GetOrCreateType(tid);
1269    }
1270  }
1271  ParseAllFunctionsAndNonLocalVars();
1272  m_parsed_namespaces.insert(ns);
1273}
1274
1275void PdbAstBuilder::ParseAllTypes() {
1276  llvm::call_once(m_parse_all_types, [this]() {
1277    SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1278        m_clang.GetSymbolFile()->GetBackingSymbolFile());
1279    PdbIndex &index = pdb->GetIndex();
1280    TypeIndex ti{index.tpi().TypeIndexBegin()};
1281    for (const CVType &cvt : index.tpi().typeArray()) {
1282      PdbTypeSymId tid{ti};
1283      ++ti;
1284
1285      if (!IsTagRecord(cvt))
1286        continue;
1287
1288      GetOrCreateType(tid);
1289    }
1290  });
1291}
1292
1293void PdbAstBuilder::ParseAllFunctionsAndNonLocalVars() {
1294  llvm::call_once(m_parse_functions_and_non_local_vars, [this]() {
1295    SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1296        m_clang.GetSymbolFile()->GetBackingSymbolFile());
1297    PdbIndex &index = pdb->GetIndex();
1298    uint32_t module_count = index.dbi().modules().getModuleCount();
1299    for (uint16_t modi = 0; modi < module_count; ++modi) {
1300      CompilandIndexItem &cii = index.compilands().GetOrCreateCompiland(modi);
1301      const CVSymbolArray &symbols = cii.m_debug_stream.getSymbolArray();
1302      auto iter = symbols.begin();
1303      while (iter != symbols.end()) {
1304        PdbCompilandSymId sym_id{modi, iter.offset()};
1305
1306        switch (iter->kind()) {
1307        case S_GPROC32:
1308        case S_LPROC32:
1309          GetOrCreateFunctionDecl(sym_id);
1310          iter = symbols.at(getScopeEndOffset(*iter));
1311          break;
1312        case S_GDATA32:
1313        case S_GTHREAD32:
1314        case S_LDATA32:
1315        case S_LTHREAD32:
1316          GetOrCreateVariableDecl(PdbCompilandSymId(modi, 0), sym_id);
1317          ++iter;
1318          break;
1319        default:
1320          ++iter;
1321          continue;
1322        }
1323      }
1324    }
1325  });
1326}
1327
1328static CVSymbolArray skipFunctionParameters(clang::Decl &decl,
1329                                            const CVSymbolArray &symbols) {
1330  clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>(&decl);
1331  if (!func_decl)
1332    return symbols;
1333  unsigned int params = func_decl->getNumParams();
1334  if (params == 0)
1335    return symbols;
1336
1337  CVSymbolArray result = symbols;
1338
1339  while (!result.empty()) {
1340    if (params == 0)
1341      return result;
1342
1343    CVSymbol sym = *result.begin();
1344    result.drop_front();
1345
1346    if (!isLocalVariableType(sym.kind()))
1347      continue;
1348
1349    --params;
1350  }
1351  return result;
1352}
1353
1354void PdbAstBuilder::ParseBlockChildren(PdbCompilandSymId block_id) {
1355  SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1356      m_clang.GetSymbolFile()->GetBackingSymbolFile());
1357  PdbIndex &index = pdb->GetIndex();
1358  CVSymbol sym = index.ReadSymbolRecord(block_id);
1359  lldbassert(sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32 ||
1360             sym.kind() == S_BLOCK32 || sym.kind() == S_INLINESITE);
1361  CompilandIndexItem &cii =
1362      index.compilands().GetOrCreateCompiland(block_id.modi);
1363  CVSymbolArray symbols =
1364      cii.m_debug_stream.getSymbolArrayForScope(block_id.offset);
1365
1366  // Function parameters should already have been created when the function was
1367  // parsed.
1368  if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32)
1369    symbols =
1370        skipFunctionParameters(*m_uid_to_decl[toOpaqueUid(block_id)], symbols);
1371
1372  symbols.drop_front();
1373  auto begin = symbols.begin();
1374  while (begin != symbols.end()) {
1375    PdbCompilandSymId child_sym_id(block_id.modi, begin.offset());
1376    GetOrCreateSymbolForId(child_sym_id);
1377    if (begin->kind() == S_BLOCK32 || begin->kind() == S_INLINESITE) {
1378      ParseBlockChildren(child_sym_id);
1379      begin = symbols.at(getScopeEndOffset(*begin));
1380    }
1381    ++begin;
1382  }
1383}
1384
1385void PdbAstBuilder::ParseDeclsForSimpleContext(clang::DeclContext &context) {
1386
1387  clang::Decl *decl = clang::Decl::castFromDeclContext(&context);
1388  lldbassert(decl);
1389
1390  auto iter = m_decl_to_status.find(decl);
1391  lldbassert(iter != m_decl_to_status.end());
1392
1393  if (auto *tag = llvm::dyn_cast<clang::TagDecl>(&context)) {
1394    CompleteTagDecl(*tag);
1395    return;
1396  }
1397
1398  if (isFunctionDecl(context) || isBlockDecl(context)) {
1399    PdbCompilandSymId block_id = PdbSymUid(iter->second.uid).asCompilandSym();
1400    ParseBlockChildren(block_id);
1401  }
1402}
1403
1404void PdbAstBuilder::ParseDeclsForContext(clang::DeclContext &context) {
1405  // Namespaces aren't explicitly represented in the debug info, and the only
1406  // way to parse them is to parse all type info, demangling every single type
1407  // and trying to reconstruct the DeclContext hierarchy this way.  Since this
1408  // is an expensive operation, we have to special case it so that we do other
1409  // work (such as parsing the items that appear within the namespaces) at the
1410  // same time.
1411  if (context.isTranslationUnit()) {
1412    ParseAllTypes();
1413    ParseAllFunctionsAndNonLocalVars();
1414    return;
1415  }
1416
1417  if (context.isNamespace()) {
1418    ParseNamespace(context);
1419    return;
1420  }
1421
1422  if (isTagDecl(context) || isFunctionDecl(context) || isBlockDecl(context)) {
1423    ParseDeclsForSimpleContext(context);
1424    return;
1425  }
1426}
1427
1428CompilerDecl PdbAstBuilder::ToCompilerDecl(clang::Decl &decl) {
1429  return m_clang.GetCompilerDecl(&decl);
1430}
1431
1432CompilerType PdbAstBuilder::ToCompilerType(clang::QualType qt) {
1433  return {m_clang.weak_from_this(), qt.getAsOpaquePtr()};
1434}
1435
1436CompilerDeclContext
1437PdbAstBuilder::ToCompilerDeclContext(clang::DeclContext &context) {
1438  return m_clang.CreateDeclContext(&context);
1439}
1440
1441clang::Decl * PdbAstBuilder::FromCompilerDecl(CompilerDecl decl) {
1442  return ClangUtil::GetDecl(decl);
1443}
1444
1445clang::DeclContext *
1446PdbAstBuilder::FromCompilerDeclContext(CompilerDeclContext context) {
1447  return static_cast<clang::DeclContext *>(context.GetOpaqueDeclContext());
1448}
1449
1450void PdbAstBuilder::Dump(Stream &stream) {
1451  m_clang.Dump(stream.AsRawOstream());
1452}
1453