1 //===-- PDBASTParser.cpp ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "PDBASTParser.h"
11 
12 #include "clang/AST/CharUnits.h"
13 #include "clang/AST/Decl.h"
14 #include "clang/AST/DeclCXX.h"
15 
16 #include "lldb/Symbol/ClangASTContext.h"
17 #include "lldb/Symbol/ClangUtil.h"
18 #include "lldb/Symbol/Declaration.h"
19 #include "lldb/Symbol/SymbolFile.h"
20 #include "lldb/Symbol/TypeSystem.h"
21 
22 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
23 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
26 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
27 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
28 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
29 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
30 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h"
31 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
32 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
33 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
34 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
35 
36 using namespace lldb;
37 using namespace lldb_private;
38 using namespace llvm::pdb;
39 
40 namespace {
41 int TranslateUdtKind(PDB_UdtType pdb_kind) {
42   switch (pdb_kind) {
43   case PDB_UdtType::Class:
44     return clang::TTK_Class;
45   case PDB_UdtType::Struct:
46     return clang::TTK_Struct;
47   case PDB_UdtType::Union:
48     return clang::TTK_Union;
49   case PDB_UdtType::Interface:
50     return clang::TTK_Interface;
51   }
52   return -1;
53 }
54 
55 lldb::Encoding TranslateBuiltinEncoding(PDB_BuiltinType type) {
56   switch (type) {
57   case PDB_BuiltinType::Float:
58     return lldb::eEncodingIEEE754;
59   case PDB_BuiltinType::Int:
60   case PDB_BuiltinType::Long:
61   case PDB_BuiltinType::Char:
62   case PDB_BuiltinType::Char16:
63   case PDB_BuiltinType::Char32:
64     return lldb::eEncodingSint;
65   case PDB_BuiltinType::Bool:
66   case PDB_BuiltinType::UInt:
67   case PDB_BuiltinType::ULong:
68   case PDB_BuiltinType::HResult:
69     return lldb::eEncodingUint;
70   default:
71     return lldb::eEncodingInvalid;
72   }
73 }
74 
75 lldb::Encoding TranslateEnumEncoding(PDB_VariantType type) {
76   switch (type) {
77   case PDB_VariantType::Int8:
78   case PDB_VariantType::Int16:
79   case PDB_VariantType::Int32:
80   case PDB_VariantType::Int64:
81     return lldb::eEncodingSint;
82 
83   case PDB_VariantType::UInt8:
84   case PDB_VariantType::UInt16:
85   case PDB_VariantType::UInt32:
86   case PDB_VariantType::UInt64:
87     return lldb::eEncodingUint;
88 
89   default:
90     break;
91   }
92 
93   return lldb::eEncodingSint;
94 }
95 
96 CompilerType
97 GetBuiltinTypeForPDBEncodingAndBitSize(ClangASTContext &clang_ast,
98                                        const PDBSymbolTypeBuiltin &pdb_type,
99                                        Encoding encoding, uint32_t width) {
100   auto *ast = clang_ast.getASTContext();
101   if (!ast)
102     return CompilerType();
103 
104   switch (pdb_type.getBuiltinType()) {
105   default:
106     break;
107   case PDB_BuiltinType::None:
108     return CompilerType();
109   case PDB_BuiltinType::Void:
110     return clang_ast.GetBasicType(eBasicTypeVoid);
111   case PDB_BuiltinType::Bool:
112     return clang_ast.GetBasicType(eBasicTypeBool);
113   case PDB_BuiltinType::Long:
114     if (width == ast->getTypeSize(ast->LongTy))
115       return CompilerType(ast, ast->LongTy);
116     if (width == ast->getTypeSize(ast->LongLongTy))
117       return CompilerType(ast, ast->LongLongTy);
118     break;
119   case PDB_BuiltinType::ULong:
120     if (width == ast->getTypeSize(ast->UnsignedLongTy))
121       return CompilerType(ast, ast->UnsignedLongTy);
122     if (width == ast->getTypeSize(ast->UnsignedLongLongTy))
123       return CompilerType(ast, ast->UnsignedLongLongTy);
124     break;
125   case PDB_BuiltinType::WCharT:
126     if (width == ast->getTypeSize(ast->WCharTy))
127       return CompilerType(ast, ast->WCharTy);
128     break;
129   case PDB_BuiltinType::Char16:
130     return CompilerType(ast, ast->Char16Ty);
131   case PDB_BuiltinType::Char32:
132     return CompilerType(ast, ast->Char32Ty);
133   case PDB_BuiltinType::Float:
134     // Note: types `long double` and `double` have same bit size in MSVC and
135     // there is no information in the PDB to distinguish them. So when falling
136     // back to default search, the compiler type of `long double` will be
137     // represented by the one generated for `double`.
138     break;
139   }
140   // If there is no match on PDB_BuiltinType, fall back to default search
141   // by encoding and width only
142   return clang_ast.GetBuiltinTypeForEncodingAndBitSize(encoding, width);
143 }
144 
145 ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin &pdb_type,
146                                   CompilerType &compiler_type) {
147   PDB_BuiltinType kind = pdb_type.getBuiltinType();
148   switch (kind) {
149   default:
150     break;
151   case PDB_BuiltinType::Currency:
152     return ConstString("CURRENCY");
153   case PDB_BuiltinType::Date:
154     return ConstString("DATE");
155   case PDB_BuiltinType::Variant:
156     return ConstString("VARIANT");
157   case PDB_BuiltinType::Complex:
158     return ConstString("complex");
159   case PDB_BuiltinType::Bitfield:
160     return ConstString("bitfield");
161   case PDB_BuiltinType::BSTR:
162     return ConstString("BSTR");
163   case PDB_BuiltinType::HResult:
164     return ConstString("HRESULT");
165   case PDB_BuiltinType::BCD:
166     return ConstString("BCD");
167   case PDB_BuiltinType::Char16:
168     return ConstString("char16_t");
169   case PDB_BuiltinType::Char32:
170     return ConstString("char32_t");
171   case PDB_BuiltinType::None:
172     return ConstString("...");
173   }
174   return compiler_type.GetTypeName();
175 }
176 
177 bool GetDeclarationForSymbol(const PDBSymbol &symbol, Declaration &decl) {
178   auto &raw_sym = symbol.getRawSymbol();
179   auto first_line_up = raw_sym.getSrcLineOnTypeDefn();
180 
181   if (!first_line_up) {
182     auto lines_up = symbol.getSession().findLineNumbersByAddress(
183         raw_sym.getVirtualAddress(), raw_sym.getLength());
184     if (!lines_up)
185       return false;
186     first_line_up = lines_up->getNext();
187     if (!first_line_up)
188       return false;
189   }
190   uint32_t src_file_id = first_line_up->getSourceFileId();
191   auto src_file_up = symbol.getSession().getSourceFileById(src_file_id);
192   if (!src_file_up)
193     return false;
194 
195   FileSpec spec(src_file_up->getFileName(), /*resolve_path*/ false);
196   decl.SetFile(spec);
197   decl.SetColumn(first_line_up->getColumnNumber());
198   decl.SetLine(first_line_up->getLineNumber());
199   return true;
200 }
201 } // namespace
202 
203 PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {}
204 
205 PDBASTParser::~PDBASTParser() {}
206 
207 // DebugInfoASTParser interface
208 
209 lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
210   // PDB doesn't maintain enough information to robustly rebuild the entire
211   // tree, and this is most problematic when it comes to figure out the
212   // right DeclContext to put a type in.  So for now, everything goes in
213   // the translation unit decl as a fully qualified type.
214   clang::DeclContext *tu_decl_ctx = m_ast.GetTranslationUnitDecl();
215   Declaration decl;
216 
217   switch (type.getSymTag()) {
218   case PDB_SymType::UDT: {
219     auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type);
220     assert(udt);
221     AccessType access = lldb::eAccessPublic;
222     PDB_UdtType udt_kind = udt->getUdtKind();
223     auto tag_type_kind = TranslateUdtKind(udt_kind);
224     if (tag_type_kind == -1)
225       return nullptr;
226 
227     if (udt_kind == PDB_UdtType::Class)
228       access = lldb::eAccessPrivate;
229 
230     CompilerType clang_type = m_ast.CreateRecordType(
231         tu_decl_ctx, access, udt->getName().c_str(), tag_type_kind,
232         lldb::eLanguageTypeC_plus_plus, nullptr);
233 
234     m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true);
235 
236     return std::make_shared<lldb_private::Type>(
237         type.getSymIndexId(), m_ast.GetSymbolFile(),
238         ConstString(udt->getName()), udt->getLength(), nullptr,
239         LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, clang_type,
240         lldb_private::Type::eResolveStateForward);
241   } break;
242   case PDB_SymType::Enum: {
243     auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type);
244     assert(enum_type);
245     auto underlying_type_up = enum_type->getUnderlyingType();
246     if (!underlying_type_up)
247       return nullptr;
248     lldb::Encoding encoding =
249         TranslateBuiltinEncoding(underlying_type_up->getBuiltinType());
250     // FIXME: Type of underlying builtin is always `Int`. We correct it with
251     // the very first enumerator's encoding if any.
252     auto first_child = enum_type->findOneChild<PDBSymbolData>();
253     if (first_child) {
254       encoding = TranslateEnumEncoding(first_child->getValue().Type);
255     }
256     std::string name = enum_type->getName();
257     uint64_t bytes = enum_type->getLength();
258     CompilerType builtin_type;
259     if (bytes > 0)
260       builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize(
261           m_ast, *underlying_type_up, encoding, bytes * 8);
262     else
263       builtin_type = m_ast.GetBasicType(eBasicTypeInt);
264     // FIXME: PDB does not have information about scoped enumeration (Enum
265     // Class). Set it false for now.
266     bool isScoped = false;
267 
268     CompilerType ast_enum = m_ast.CreateEnumerationType(
269         name.c_str(), tu_decl_ctx, decl, builtin_type, isScoped);
270     auto enum_values = enum_type->findAllChildren<PDBSymbolData>();
271     if (enum_values) {
272       while (auto enum_value = enum_values->getNext()) {
273         if (enum_value->getDataKind() != PDB_DataKind::Constant)
274           continue;
275         AddEnumValue(ast_enum, *enum_value);
276       }
277     }
278     if (ClangASTContext::StartTagDeclarationDefinition(ast_enum))
279       ClangASTContext::CompleteTagDeclarationDefinition(ast_enum);
280 
281     GetDeclarationForSymbol(type, decl);
282     return std::make_shared<lldb_private::Type>(
283         type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes,
284         nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl,
285         ast_enum, lldb_private::Type::eResolveStateFull);
286   } break;
287   case PDB_SymType::Typedef: {
288     auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type);
289     assert(type_def);
290     lldb_private::Type *target_type =
291         m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId());
292     if (!target_type)
293       return nullptr;
294     std::string name = type_def->getName();
295     uint64_t bytes = type_def->getLength();
296     CompilerType target_ast_type = target_type->GetFullCompilerType();
297     CompilerDeclContext target_decl_ctx =
298         m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID());
299     CompilerType ast_typedef =
300         m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx);
301     if (!ast_typedef)
302       return nullptr;
303 
304     return std::make_shared<lldb_private::Type>(
305         type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name),
306         bytes, nullptr, target_type->GetID(),
307         lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef,
308         lldb_private::Type::eResolveStateFull);
309   } break;
310   case PDB_SymType::Function:
311   case PDB_SymType::FunctionSig: {
312     std::string name;
313     PDBSymbolTypeFunctionSig *func_sig = nullptr;
314     if (auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(&type)) {
315       if (pdb_func->isCompilerGenerated())
316         return nullptr;
317 
318       auto sig = pdb_func->getSignature();
319       if (!sig)
320         return nullptr;
321       func_sig = sig.release();
322       // Function type is named.
323       name = pdb_func->getName();
324     } else if (auto pdb_func_sig =
325                    llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) {
326       func_sig = const_cast<PDBSymbolTypeFunctionSig *>(pdb_func_sig);
327     } else
328       llvm_unreachable("Unexpected PDB symbol!");
329 
330     auto arg_enum = func_sig->getArguments();
331     uint32_t num_args = arg_enum->getChildCount();
332     std::vector<CompilerType> arg_list;
333 
334     bool is_variadic = func_sig->isCVarArgs();
335     // Drop last variadic argument.
336     if (is_variadic)
337       --num_args;
338     for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) {
339       auto arg = arg_enum->getChildAtIndex(arg_idx);
340       if (!arg)
341         break;
342       lldb_private::Type *arg_type =
343           m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId());
344       // If there's some error looking up one of the dependent types of this
345       // function signature, bail.
346       if (!arg_type)
347         return nullptr;
348       CompilerType arg_ast_type = arg_type->GetFullCompilerType();
349       arg_list.push_back(arg_ast_type);
350     }
351     lldbassert(arg_list.size() <= num_args);
352 
353     auto pdb_return_type = func_sig->getReturnType();
354     lldb_private::Type *return_type =
355         m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId());
356     // If there's some error looking up one of the dependent types of this
357     // function signature, bail.
358     if (!return_type)
359       return nullptr;
360     CompilerType return_ast_type = return_type->GetFullCompilerType();
361     uint32_t type_quals = 0;
362     if (func_sig->isConstType())
363       type_quals |= clang::Qualifiers::Const;
364     if (func_sig->isVolatileType())
365       type_quals |= clang::Qualifiers::Volatile;
366     CompilerType func_sig_ast_type =
367         m_ast.CreateFunctionType(return_ast_type, arg_list.data(),
368                                  arg_list.size(), is_variadic, type_quals);
369 
370     GetDeclarationForSymbol(type, decl);
371     return std::make_shared<lldb_private::Type>(
372         type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 0,
373         nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl,
374         func_sig_ast_type, lldb_private::Type::eResolveStateFull);
375   } break;
376   case PDB_SymType::ArrayType: {
377     auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type);
378     assert(array_type);
379     uint32_t num_elements = array_type->getCount();
380     uint32_t element_uid = array_type->getElementTypeId();
381     uint32_t bytes = array_type->getLength();
382 
383     // If array rank > 0, PDB gives the element type at N=0. So element type
384     // will parsed in the order N=0, N=1,..., N=rank sequentially.
385     lldb_private::Type *element_type =
386         m_ast.GetSymbolFile()->ResolveTypeUID(element_uid);
387     if (!element_type)
388       return nullptr;
389 
390     CompilerType element_ast_type = element_type->GetForwardCompilerType();
391     // If element type is UDT, it needs to be complete.
392     if (ClangASTContext::IsCXXClassType(element_ast_type) &&
393         element_ast_type.GetCompleteType() == false) {
394       if (ClangASTContext::StartTagDeclarationDefinition(element_ast_type)) {
395         ClangASTContext::CompleteTagDeclarationDefinition(element_ast_type);
396       } else {
397         // We are not able to start defintion.
398         return nullptr;
399       }
400     }
401     CompilerType array_ast_type = m_ast.CreateArrayType(
402         element_ast_type, num_elements, /*is_gnu_vector*/ false);
403     TypeSP type_sp = std::make_shared<lldb_private::Type>(
404         array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(),
405         bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID,
406         decl, array_ast_type, lldb_private::Type::eResolveStateFull);
407     type_sp->SetEncodingType(element_type);
408     return type_sp;
409   } break;
410   case PDB_SymType::BuiltinType: {
411     auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type);
412     assert(builtin_type);
413     PDB_BuiltinType builtin_kind = builtin_type->getBuiltinType();
414     if (builtin_kind == PDB_BuiltinType::None)
415       return nullptr;
416 
417     uint64_t bytes = builtin_type->getLength();
418     Encoding encoding = TranslateBuiltinEncoding(builtin_kind);
419     CompilerType builtin_ast_type = GetBuiltinTypeForPDBEncodingAndBitSize(
420         m_ast, *builtin_type, encoding, bytes * 8);
421 
422     if (builtin_type->isConstType())
423       builtin_ast_type = builtin_ast_type.AddConstModifier();
424 
425     if (builtin_type->isVolatileType())
426       builtin_ast_type = builtin_ast_type.AddVolatileModifier();
427 
428     auto type_name = GetPDBBuiltinTypeName(*builtin_type, builtin_ast_type);
429 
430     return std::make_shared<lldb_private::Type>(
431         builtin_type->getSymIndexId(), m_ast.GetSymbolFile(), type_name, bytes,
432         nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl,
433         builtin_ast_type, lldb_private::Type::eResolveStateFull);
434   } break;
435   case PDB_SymType::PointerType: {
436     auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type);
437     assert(pointer_type);
438     Type *pointee_type = m_ast.GetSymbolFile()->ResolveTypeUID(
439         pointer_type->getPointeeType()->getSymIndexId());
440     if (!pointee_type)
441       return nullptr;
442 
443     CompilerType pointer_ast_type;
444     pointer_ast_type = pointee_type->GetFullCompilerType();
445     if (pointer_type->isReference())
446       pointer_ast_type = pointer_ast_type.GetLValueReferenceType();
447     else if (pointer_type->isRValueReference())
448       pointer_ast_type = pointer_ast_type.GetRValueReferenceType();
449     else
450       pointer_ast_type = pointer_ast_type.GetPointerType();
451 
452     if (pointer_type->isConstType())
453       pointer_ast_type = pointer_ast_type.AddConstModifier();
454 
455     if (pointer_type->isVolatileType())
456       pointer_ast_type = pointer_ast_type.AddVolatileModifier();
457 
458     if (pointer_type->isRestrictedType())
459       pointer_ast_type = pointer_ast_type.AddRestrictModifier();
460 
461     return std::make_shared<lldb_private::Type>(
462         pointer_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(),
463         pointer_type->getLength(), nullptr, LLDB_INVALID_UID,
464         lldb_private::Type::eEncodingIsUID, decl, pointer_ast_type,
465         lldb_private::Type::eResolveStateFull);
466   } break;
467   default:
468     break;
469   }
470   return nullptr;
471 }
472 
473 bool PDBASTParser::AddEnumValue(CompilerType enum_type,
474                                 const PDBSymbolData &enum_value) const {
475   Declaration decl;
476   Variant v = enum_value.getValue();
477   std::string name = enum_value.getName();
478   int64_t raw_value;
479   switch (v.Type) {
480   case PDB_VariantType::Int8:
481     raw_value = v.Value.Int8;
482     break;
483   case PDB_VariantType::Int16:
484     raw_value = v.Value.Int16;
485     break;
486   case PDB_VariantType::Int32:
487     raw_value = v.Value.Int32;
488     break;
489   case PDB_VariantType::Int64:
490     raw_value = v.Value.Int64;
491     break;
492   case PDB_VariantType::UInt8:
493     raw_value = v.Value.UInt8;
494     break;
495   case PDB_VariantType::UInt16:
496     raw_value = v.Value.UInt16;
497     break;
498   case PDB_VariantType::UInt32:
499     raw_value = v.Value.UInt32;
500     break;
501   case PDB_VariantType::UInt64:
502     raw_value = v.Value.UInt64;
503     break;
504   default:
505     return false;
506   }
507   CompilerType underlying_type =
508       m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
509   uint32_t byte_size = m_ast.getASTContext()->getTypeSize(
510       ClangUtil::GetQualType(underlying_type));
511   return m_ast.AddEnumerationValueToEnumerationType(
512       enum_type.GetOpaqueQualType(), underlying_type, decl, name.c_str(),
513       raw_value, byte_size * 8);
514 }
515