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