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