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