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     return lldb::eEncodingSint;
63   case PDB_BuiltinType::Bool:
64   case PDB_BuiltinType::UInt:
65   case PDB_BuiltinType::ULong:
66   case PDB_BuiltinType::HResult:
67     return lldb::eEncodingUint;
68   default:
69     return lldb::eEncodingInvalid;
70   }
71 }
72 
73 lldb::Encoding TranslateEnumEncoding(PDB_VariantType type) {
74   switch (type) {
75   case PDB_VariantType::Int8:
76   case PDB_VariantType::Int16:
77   case PDB_VariantType::Int32:
78   case PDB_VariantType::Int64:
79     return lldb::eEncodingSint;
80 
81   case PDB_VariantType::UInt8:
82   case PDB_VariantType::UInt16:
83   case PDB_VariantType::UInt32:
84   case PDB_VariantType::UInt64:
85     return lldb::eEncodingUint;
86 
87   default:
88     break;
89   }
90 
91   return lldb::eEncodingSint;
92 }
93 
94 CompilerType GetBuiltinTypeForPDBEncodingAndBitSize(
95     ClangASTContext *clang_ast, const PDBSymbolTypeBuiltin *pdb_type,
96     Encoding encoding, uint32_t width) {
97   if (!pdb_type)
98     return CompilerType();
99   if (!clang_ast)
100     return CompilerType();
101   auto *ast = clang_ast->getASTContext();
102   if (!ast)
103     return CompilerType();
104 
105   switch (pdb_type->getBuiltinType()) {
106   default: break;
107   case PDB_BuiltinType::None:
108     return CompilerType();
109   case PDB_BuiltinType::Void:
110     // FIXME: where is non-zero size of `void` from?
111     if (width == 0)
112       return clang_ast->GetBasicType(eBasicTypeVoid);
113   case PDB_BuiltinType::Bool:
114     return clang_ast->GetBasicType(eBasicTypeBool);
115   case PDB_BuiltinType::Long:
116     if (width == ast->getTypeSize(ast->LongTy))
117       return CompilerType(ast, ast->LongTy);
118     if (width == ast->getTypeSize(ast->LongLongTy))
119       return CompilerType(ast, ast->LongLongTy);
120     break;
121   case PDB_BuiltinType::ULong:
122     if (width == ast->getTypeSize(ast->UnsignedLongTy))
123       return CompilerType(ast, ast->UnsignedLongTy);
124     if (width == ast->getTypeSize(ast->UnsignedLongLongTy))
125       return CompilerType(ast, ast->UnsignedLongLongTy);
126     break;
127   case PDB_BuiltinType::WCharT:
128     if (width == ast->getTypeSize(ast->WCharTy))
129       return CompilerType(ast, ast->WCharTy);
130     break;
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   if (!pdb_type)
146     return compiler_type.GetTypeName();
147 
148   PDB_BuiltinType kind = pdb_type->getBuiltinType();
149   switch (kind) {
150   default: 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::None:
168     return ConstString("...");
169   }
170   return compiler_type.GetTypeName();
171 }
172 
173 bool GetDeclarationForSymbol(const PDBSymbol &symbol, Declaration &decl) {
174   auto &raw_sym = symbol.getRawSymbol();
175   auto lines_up = symbol.getSession().findLineNumbersByAddress(
176       raw_sym.getVirtualAddress(), raw_sym.getLength());
177   if (!lines_up)
178     return false;
179   auto first_line_up = lines_up->getNext();
180   if (!first_line_up)
181     return false;
182 
183   uint32_t src_file_id = first_line_up->getSourceFileId();
184   auto src_file_up = symbol.getSession().getSourceFileById(src_file_id);
185   if (!src_file_up)
186     return false;
187 
188   FileSpec spec(src_file_up->getFileName(), /*resolve_path*/false);
189   decl.SetFile(spec);
190   decl.SetColumn(first_line_up->getColumnNumber());
191   decl.SetLine(first_line_up->getLineNumber());
192   return true;
193 }
194 }
195 
196 PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {}
197 
198 PDBASTParser::~PDBASTParser() {}
199 
200 // DebugInfoASTParser interface
201 
202 lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
203   // PDB doesn't maintain enough information to robustly rebuild the entire
204   // tree, and this is most problematic when it comes to figure out the
205   // right DeclContext to put a type in.  So for now, everything goes in
206   // the translation unit decl as a fully qualified type.
207   clang::DeclContext *tu_decl_ctx = m_ast.GetTranslationUnitDecl();
208   Declaration decl;
209 
210   switch (type.getSymTag()) {
211   case PDB_SymType::UDT: {
212     auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type);
213     assert(udt);
214     AccessType access = lldb::eAccessPublic;
215     PDB_UdtType udt_kind = udt->getUdtKind();
216     auto tag_type_kind = TranslateUdtKind(udt_kind);
217     if (tag_type_kind == -1)
218       return nullptr;
219 
220     if (udt_kind == PDB_UdtType::Class)
221       access = lldb::eAccessPrivate;
222 
223     CompilerType clang_type = m_ast.CreateRecordType(
224         tu_decl_ctx, access, udt->getName().c_str(), tag_type_kind,
225         lldb::eLanguageTypeC_plus_plus, nullptr);
226 
227     m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true);
228 
229     return std::make_shared<lldb_private::Type>(
230         type.getSymIndexId(), m_ast.GetSymbolFile(),
231         ConstString(udt->getName()), udt->getLength(), nullptr,
232         LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, clang_type,
233         lldb_private::Type::eResolveStateForward);
234   } break;
235   case PDB_SymType::Enum: {
236     auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type);
237     assert(enum_type);
238     auto underlying_type_up = enum_type->getUnderlyingType();
239     if (!underlying_type_up)
240       return nullptr;
241     lldb::Encoding encoding =
242         TranslateBuiltinEncoding(underlying_type_up->getBuiltinType());
243     // FIXME: Type of underlying builtin is always `Int`. We correct it with
244     // the very first enumerator's encoding if any.
245     auto first_child = enum_type->findOneChild<PDBSymbolData>();
246     if (first_child) {
247       encoding = TranslateEnumEncoding(first_child->getValue().Type);
248     }
249     std::string name = enum_type->getName();
250     uint64_t bytes = enum_type->getLength();
251     CompilerType builtin_type;
252     if (bytes > 0)
253       builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize(
254            &m_ast, underlying_type_up.get(), encoding, bytes * 8);
255     else
256       builtin_type = m_ast.GetBasicType(eBasicTypeInt);
257     // FIXME: PDB does not have information about scoped enumeration (Enum Class).
258     // Set it false for now.
259     bool isScoped = false;
260 
261     CompilerType ast_enum = m_ast.CreateEnumerationType(
262         name.c_str(), tu_decl_ctx, decl, builtin_type, isScoped);
263     auto enum_values = enum_type->findAllChildren<PDBSymbolData>();
264     if (enum_values) {
265       while (auto enum_value = enum_values->getNext()) {
266         if (enum_value->getDataKind() != PDB_DataKind::Constant)
267           continue;
268         AddEnumValue(ast_enum, *enum_value);
269       }
270     }
271     if (ClangASTContext::StartTagDeclarationDefinition(ast_enum))
272       ClangASTContext::CompleteTagDeclarationDefinition(ast_enum);
273 
274     return std::make_shared<lldb_private::Type>(
275         type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes,
276         nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl,
277         ast_enum, lldb_private::Type::eResolveStateFull);
278   } break;
279   case PDB_SymType::Typedef: {
280     auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type);
281     assert(type_def);
282     lldb_private::Type *target_type =
283         m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId());
284     if (!target_type)
285       return nullptr;
286     std::string name = type_def->getName();
287     uint64_t bytes = type_def->getLength();
288     CompilerType target_ast_type = target_type->GetFullCompilerType();
289     CompilerDeclContext target_decl_ctx =
290         m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID());
291     CompilerType ast_typedef =
292         m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx);
293     return std::make_shared<lldb_private::Type>(
294         type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name),
295         bytes, nullptr, target_type->GetID(),
296         lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef,
297         lldb_private::Type::eResolveStateFull);
298   } break;
299   case PDB_SymType::Function:
300   case PDB_SymType::FunctionSig: {
301     std::string name;
302     PDBSymbolTypeFunctionSig *func_sig = nullptr;
303     if (auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(&type)) {
304       auto sig = pdb_func->getSignature();
305       if (!sig)
306         return nullptr;
307       func_sig = sig.release();
308       // Function type is named.
309       name = pdb_func->getName();
310     } else if (auto pdb_func_sig =
311               llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) {
312       func_sig = const_cast<PDBSymbolTypeFunctionSig*>(pdb_func_sig);
313     } else
314       llvm_unreachable("Unexpected PDB symbol!");
315 
316     auto arg_enum = func_sig->getArguments();
317     uint32_t num_args = arg_enum->getChildCount();
318     std::vector<CompilerType> arg_list;
319 
320     bool is_variadic = func_sig->isCVarArgs();
321     // Drop last variadic argument.
322     if (is_variadic)
323       --num_args;
324     for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) {
325       auto arg = arg_enum->getChildAtIndex(arg_idx);
326       if (!arg)
327         break;
328       lldb_private::Type *arg_type =
329           m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId());
330       // If there's some error looking up one of the dependent types of this
331       // function signature, bail.
332       if (!arg_type)
333         return nullptr;
334       CompilerType arg_ast_type = arg_type->GetFullCompilerType();
335       arg_list.push_back(arg_ast_type);
336     }
337     lldbassert(arg_list.size() <= num_args);
338 
339     auto pdb_return_type = func_sig->getReturnType();
340     lldb_private::Type *return_type =
341         m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId());
342     // If there's some error looking up one of the dependent types of this
343     // function signature, bail.
344     if (!return_type)
345       return nullptr;
346     CompilerType return_ast_type = return_type->GetFullCompilerType();
347     uint32_t type_quals = 0;
348     if (func_sig->isConstType())
349       type_quals |= clang::Qualifiers::Const;
350     if (func_sig->isVolatileType())
351       type_quals |= clang::Qualifiers::Volatile;
352     CompilerType func_sig_ast_type = m_ast.CreateFunctionType(
353         return_ast_type, arg_list.data(), arg_list.size(), is_variadic,
354         type_quals);
355 
356     GetDeclarationForSymbol(type, decl);
357     return std::make_shared<lldb_private::Type>(
358         type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 0,
359         nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl,
360         func_sig_ast_type, lldb_private::Type::eResolveStateFull);
361   } break;
362   case PDB_SymType::ArrayType: {
363     auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type);
364     assert(array_type);
365     uint32_t num_elements = array_type->getCount();
366     uint32_t element_uid = array_type->getElementType()->getSymIndexId();
367     uint32_t bytes = array_type->getLength();
368 
369     lldb_private::Type *element_type =
370         m_ast.GetSymbolFile()->ResolveTypeUID(element_uid);
371     if (!element_type)
372       return nullptr;
373     CompilerType element_ast_type = element_type->GetFullCompilerType();
374     CompilerType array_ast_type =
375         m_ast.CreateArrayType(element_ast_type, num_elements, false);
376     return std::make_shared<lldb_private::Type>(
377         array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(),
378         bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID,
379         decl, array_ast_type, lldb_private::Type::eResolveStateFull);
380   } break;
381   case PDB_SymType::BuiltinType: {
382     auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type);
383     assert(builtin_type);
384     PDB_BuiltinType builtin_kind = builtin_type->getBuiltinType();
385     if (builtin_kind == PDB_BuiltinType::None)
386       return nullptr;
387 
388     uint64_t bytes = builtin_type->getLength();
389     Encoding encoding = TranslateBuiltinEncoding(builtin_kind);
390     CompilerType builtin_ast_type = GetBuiltinTypeForPDBEncodingAndBitSize(
391         &m_ast, builtin_type, encoding, bytes * 8);
392 
393     Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
394     if (builtin_type->isConstType()) {
395       encoding_data_type = Type::eEncodingIsConstUID;
396       builtin_ast_type = builtin_ast_type.AddConstModifier();
397     }
398     if (builtin_type->isVolatileType()) {
399       encoding_data_type = Type::eEncodingIsVolatileUID;
400       builtin_ast_type = builtin_ast_type.AddVolatileModifier();
401     }
402     auto type_name = GetPDBBuiltinTypeName(builtin_type, builtin_ast_type);
403 
404     return std::make_shared<lldb_private::Type>(
405         builtin_type->getSymIndexId(), m_ast.GetSymbolFile(), type_name,
406         bytes, nullptr, LLDB_INVALID_UID, encoding_data_type,
407         decl, builtin_ast_type, lldb_private::Type::eResolveStateFull);
408   } break;
409   case PDB_SymType::PointerType: {
410     auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type);
411     assert(pointer_type);
412     Type *pointee_type = m_ast.GetSymbolFile()->ResolveTypeUID(
413         pointer_type->getPointeeType()->getSymIndexId());
414     if (!pointee_type)
415       return nullptr;
416 
417     CompilerType pointer_ast_type;
418     Type::EncodingDataType encoding_data_type = Type::eEncodingIsPointerUID;
419     if (pointer_type->isReference()) {
420       encoding_data_type = Type::eEncodingIsLValueReferenceUID;
421       pointer_ast_type =
422           pointee_type->GetFullCompilerType().GetLValueReferenceType();
423     } else
424       pointer_ast_type = pointee_type->GetFullCompilerType().GetPointerType();
425 
426     return std::make_shared<lldb_private::Type>(
427         pointer_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(),
428         pointer_type->getLength(), nullptr, LLDB_INVALID_UID,
429         encoding_data_type, decl, pointer_ast_type,
430         lldb_private::Type::eResolveStateFull);
431   } break;
432   default: break;
433   }
434   return nullptr;
435 }
436 
437 bool PDBASTParser::AddEnumValue(CompilerType enum_type,
438                                 const PDBSymbolData &enum_value) const {
439   Declaration decl;
440   Variant v = enum_value.getValue();
441   std::string name = enum_value.getName();
442   int64_t raw_value;
443   switch (v.Type) {
444   case PDB_VariantType::Int8:
445     raw_value = v.Value.Int8;
446     break;
447   case PDB_VariantType::Int16:
448     raw_value = v.Value.Int16;
449     break;
450   case PDB_VariantType::Int32:
451     raw_value = v.Value.Int32;
452     break;
453   case PDB_VariantType::Int64:
454     raw_value = v.Value.Int64;
455     break;
456   case PDB_VariantType::UInt8:
457     raw_value = v.Value.UInt8;
458     break;
459   case PDB_VariantType::UInt16:
460     raw_value = v.Value.UInt16;
461     break;
462   case PDB_VariantType::UInt32:
463     raw_value = v.Value.UInt32;
464     break;
465   case PDB_VariantType::UInt64:
466     raw_value = v.Value.UInt64;
467     break;
468   default:
469     return false;
470   }
471   CompilerType underlying_type =
472       m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
473   uint32_t byte_size = m_ast.getASTContext()->getTypeSize(
474       ClangUtil::GetQualType(underlying_type));
475   return m_ast.AddEnumerationValueToEnumerationType(
476       enum_type.GetOpaqueQualType(), underlying_type, decl, name.c_str(),
477       raw_value, byte_size * 8);
478 }
479