blob: 9b98ebe112a24c36c5c3551893bc1a032a62c8bd [file] [log] [blame]
Zachary Turner42dff792016-04-15 00:21:26 +00001//===-- 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/PDBSymbol.h"
23#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
24#include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
25#include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
26#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
27#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h"
28#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
29#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
30#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
31
32using namespace lldb;
33using namespace lldb_private;
34using namespace llvm;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000035using namespace llvm::pdb;
Zachary Turner42dff792016-04-15 00:21:26 +000036
Kate Stoneb9c1b512016-09-06 20:57:50 +000037namespace {
38int TranslateUdtKind(PDB_UdtType pdb_kind) {
39 switch (pdb_kind) {
40 case PDB_UdtType::Class:
Zachary Turner42dff792016-04-15 00:21:26 +000041 return clang::TTK_Class;
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 case PDB_UdtType::Struct:
43 return clang::TTK_Struct;
44 case PDB_UdtType::Union:
45 return clang::TTK_Union;
46 case PDB_UdtType::Interface:
47 return clang::TTK_Interface;
48 }
49 return clang::TTK_Class;
Zachary Turner42dff792016-04-15 00:21:26 +000050}
51
Kate Stoneb9c1b512016-09-06 20:57:50 +000052lldb::Encoding TranslateBuiltinEncoding(PDB_BuiltinType type) {
53 switch (type) {
54 case PDB_BuiltinType::Float:
55 return lldb::eEncodingIEEE754;
56 case PDB_BuiltinType::Int:
57 case PDB_BuiltinType::Long:
58 case PDB_BuiltinType::Char:
59 return lldb::eEncodingSint;
60 case PDB_BuiltinType::Bool:
61 case PDB_BuiltinType::UInt:
62 case PDB_BuiltinType::ULong:
63 case PDB_BuiltinType::HResult:
64 return lldb::eEncodingUint;
65 default:
66 return lldb::eEncodingInvalid;
67 }
Zachary Turner42dff792016-04-15 00:21:26 +000068}
69}
70
Kate Stoneb9c1b512016-09-06 20:57:50 +000071PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {}
Zachary Turner42dff792016-04-15 00:21:26 +000072
Kate Stoneb9c1b512016-09-06 20:57:50 +000073PDBASTParser::~PDBASTParser() {}
Zachary Turner42dff792016-04-15 00:21:26 +000074
75// DebugInfoASTParser interface
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
78 // PDB doesn't maintain enough information to robustly rebuild the entire
79 // tree, and this is most problematic when it comes to figure out the
80 // right DeclContext to put a type in. So for now, everything goes in
81 // the translation unit decl as a fully qualified type.
82 clang::DeclContext *tu_decl_ctx = m_ast.GetTranslationUnitDecl();
83 Declaration decl;
Zachary Turner42dff792016-04-15 00:21:26 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 if (auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type)) {
86 AccessType access = lldb::eAccessPublic;
87 PDB_UdtType udt_kind = udt->getUdtKind();
Zachary Turner42dff792016-04-15 00:21:26 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 if (udt_kind == PDB_UdtType::Class)
90 access = lldb::eAccessPrivate;
Zachary Turner42dff792016-04-15 00:21:26 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 CompilerType clang_type = m_ast.CreateRecordType(
93 tu_decl_ctx, access, udt->getName().c_str(), TranslateUdtKind(udt_kind),
94 lldb::eLanguageTypeC_plus_plus, nullptr);
Zachary Turner42dff792016-04-15 00:21:26 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true);
Zachary Turner42dff792016-04-15 00:21:26 +000097
Saleem Abdulrasool6c135102017-09-09 00:13:49 +000098 return std::make_shared<lldb_private::Type>(
99 type.getSymIndexId(), m_ast.GetSymbolFile(),
100 ConstString(udt->getName()), udt->getLength(), nullptr,
101 LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, clang_type,
102 lldb_private::Type::eResolveStateForward);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 } else if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type)) {
104 std::string name = enum_type->getName();
105 lldb::Encoding encoding =
106 TranslateBuiltinEncoding(enum_type->getBuiltinType());
107 uint64_t bytes = enum_type->getLength();
108 CompilerType builtin_type =
109 m_ast.GetBuiltinTypeForEncodingAndBitSize(encoding, bytes * 8);
110
111 CompilerType ast_enum = m_ast.CreateEnumerationType(
Tamas Berghammer59765832017-11-07 10:39:22 +0000112 name.c_str(), tu_decl_ctx, decl, builtin_type, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 auto enum_values = enum_type->findAllChildren<PDBSymbolData>();
114 while (auto enum_value = enum_values->getNext()) {
115 if (enum_value->getDataKind() != PDB_DataKind::Constant)
116 continue;
117 AddEnumValue(ast_enum, *enum_value);
Zachary Turner42dff792016-04-15 00:21:26 +0000118 }
Zachary Turner42dff792016-04-15 00:21:26 +0000119
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000120 return std::make_shared<lldb_private::Type>(
121 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes,
122 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl,
123 ast_enum, lldb_private::Type::eResolveStateFull);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 } else if (auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type)) {
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000125 lldb_private::Type *target_type =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId());
127 std::string name = type_def->getName();
128 uint64_t bytes = type_def->getLength();
129 if (!target_type)
130 return nullptr;
131 CompilerType target_ast_type = target_type->GetFullCompilerType();
132 CompilerDeclContext target_decl_ctx =
133 m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID());
134 CompilerType ast_typedef =
135 m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx);
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000136 return std::make_shared<lldb_private::Type>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name),
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000138 bytes, nullptr, target_type->GetID(),
139 lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef,
140 lldb_private::Type::eResolveStateFull);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 } else if (auto func_sig = llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) {
142 auto arg_enum = func_sig->getArguments();
143 uint32_t num_args = arg_enum->getChildCount();
144 std::vector<CompilerType> arg_list(num_args);
145 while (auto arg = arg_enum->getNext()) {
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000146 lldb_private::Type *arg_type =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId());
148 // If there's some error looking up one of the dependent types of this
149 // function signature, bail.
150 if (!arg_type)
151 return nullptr;
152 CompilerType arg_ast_type = arg_type->GetFullCompilerType();
153 arg_list.push_back(arg_ast_type);
154 }
155 auto pdb_return_type = func_sig->getReturnType();
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000156 lldb_private::Type *return_type =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId());
158 // If there's some error looking up one of the dependent types of this
159 // function signature, bail.
160 if (!return_type)
161 return nullptr;
162 CompilerType return_ast_type = return_type->GetFullCompilerType();
163 uint32_t type_quals = 0;
164 if (func_sig->isConstType())
165 type_quals |= clang::Qualifiers::Const;
166 if (func_sig->isVolatileType())
167 type_quals |= clang::Qualifiers::Volatile;
168 CompilerType func_sig_ast_type = m_ast.CreateFunctionType(
169 return_ast_type, &arg_list[0], num_args, false, type_quals);
Zachary Turner42dff792016-04-15 00:21:26 +0000170
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000171 return std::make_shared<lldb_private::Type>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 func_sig->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 0,
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000173 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl,
174 func_sig_ast_type, lldb_private::Type::eResolveStateFull);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 } else if (auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type)) {
176 uint32_t num_elements = array_type->getCount();
177 uint32_t element_uid = array_type->getElementType()->getSymIndexId();
178 uint32_t bytes = array_type->getLength();
Zachary Turner42dff792016-04-15 00:21:26 +0000179
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000180 lldb_private::Type *element_type =
181 m_ast.GetSymbolFile()->ResolveTypeUID(element_uid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 CompilerType element_ast_type = element_type->GetFullCompilerType();
183 CompilerType array_ast_type =
184 m_ast.CreateArrayType(element_ast_type, num_elements, false);
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000185 return std::make_shared<lldb_private::Type>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(),
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000187 bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID,
188 decl, array_ast_type, lldb_private::Type::eResolveStateFull);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 }
190 return nullptr;
Zachary Turner42dff792016-04-15 00:21:26 +0000191}
192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193bool PDBASTParser::AddEnumValue(CompilerType enum_type,
194 const PDBSymbolData &enum_value) const {
195 Declaration decl;
196 Variant v = enum_value.getValue();
197 std::string name = enum_value.getName();
198 int64_t raw_value;
199 switch (v.Type) {
200 case PDB_VariantType::Int8:
201 raw_value = v.Value.Int8;
202 break;
203 case PDB_VariantType::Int16:
204 raw_value = v.Value.Int16;
205 break;
206 case PDB_VariantType::Int32:
207 raw_value = v.Value.Int32;
208 break;
209 case PDB_VariantType::Int64:
210 raw_value = v.Value.Int64;
211 break;
212 case PDB_VariantType::UInt8:
213 raw_value = v.Value.UInt8;
214 break;
215 case PDB_VariantType::UInt16:
216 raw_value = v.Value.UInt16;
217 break;
218 case PDB_VariantType::UInt32:
219 raw_value = v.Value.UInt32;
220 break;
221 case PDB_VariantType::UInt64:
222 raw_value = v.Value.UInt64;
223 break;
224 default:
225 return false;
226 }
227 CompilerType underlying_type =
228 m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
229 uint32_t byte_size = m_ast.getASTContext()->getTypeSize(
230 ClangUtil::GetQualType(underlying_type));
231 return m_ast.AddEnumerationValueToEnumerationType(
232 enum_type.GetOpaqueQualType(), underlying_type, decl, name.c_str(),
233 raw_value, byte_size * 8);
Zachary Turner42dff792016-04-15 00:21:26 +0000234}