blob: 1e8e040fd47c9e6c1911f7631cd14b05a73c6139 [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
37namespace
38{
39int
40TranslateUdtKind(PDB_UdtType pdb_kind)
41{
42 switch (pdb_kind)
43 {
44 case PDB_UdtType::Class:
45 return clang::TTK_Class;
46 case PDB_UdtType::Struct:
47 return clang::TTK_Struct;
48 case PDB_UdtType::Union:
49 return clang::TTK_Union;
50 case PDB_UdtType::Interface:
51 return clang::TTK_Interface;
52 }
53 return clang::TTK_Class;
54}
55
56lldb::Encoding
57TranslateBuiltinEncoding(PDB_BuiltinType type)
58{
59 switch (type)
60 {
61 case PDB_BuiltinType::Float:
62 return lldb::eEncodingIEEE754;
63 case PDB_BuiltinType::Int:
64 case PDB_BuiltinType::Long:
65 case PDB_BuiltinType::Char:
66 return lldb::eEncodingSint;
67 case PDB_BuiltinType::Bool:
68 case PDB_BuiltinType::UInt:
69 case PDB_BuiltinType::ULong:
70 case PDB_BuiltinType::HResult:
71 return lldb::eEncodingUint;
72 default:
73 return lldb::eEncodingInvalid;
74 }
75}
76}
77
78PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast)
79{
80}
81
82PDBASTParser::~PDBASTParser()
83{
84}
85
86// DebugInfoASTParser interface
87
88lldb::TypeSP
Zachary Turner54fd7ff2016-05-04 20:33:53 +000089PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type)
Zachary Turner42dff792016-04-15 00:21:26 +000090{
91 // PDB doesn't maintain enough information to robustly rebuild the entire
92 // tree, and this is most problematic when it comes to figure out the
93 // right DeclContext to put a type in. So for now, everything goes in
94 // the translation unit decl as a fully qualified type.
95 clang::DeclContext *tu_decl_ctx = m_ast.GetTranslationUnitDecl();
96 Declaration decl;
97
98 if (auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type))
99 {
100 AccessType access = lldb::eAccessPublic;
101 PDB_UdtType udt_kind = udt->getUdtKind();
102
103 if (udt_kind == PDB_UdtType::Class)
104 access = lldb::eAccessPrivate;
105
106 CompilerType clang_type =
107 m_ast.CreateRecordType(tu_decl_ctx, access, udt->getName().c_str(), TranslateUdtKind(udt_kind),
108 lldb::eLanguageTypeC_plus_plus, nullptr);
109
110 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true);
111
112 return std::make_shared<Type>(type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(udt->getName()),
113 udt->getLength(), nullptr, LLDB_INVALID_UID, Type::eEncodingIsUID, decl,
114 clang_type, Type::eResolveStateForward);
115 }
116 else if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type))
117 {
118 std::string name = enum_type->getName();
119 lldb::Encoding encoding = TranslateBuiltinEncoding(enum_type->getBuiltinType());
120 uint64_t bytes = enum_type->getLength();
121 CompilerType builtin_type = m_ast.GetBuiltinTypeForEncodingAndBitSize(encoding, bytes * 8);
122
123 CompilerType ast_enum = m_ast.CreateEnumerationType(name.c_str(), tu_decl_ctx, decl, builtin_type);
124 auto enum_values = enum_type->findAllChildren<PDBSymbolData>();
125 while (auto enum_value = enum_values->getNext())
126 {
127 if (enum_value->getDataKind() != PDB_DataKind::Constant)
128 continue;
129 AddEnumValue(ast_enum, *enum_value);
130 }
131
132 return std::make_shared<Type>(type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes, nullptr,
133 LLDB_INVALID_UID, Type::eEncodingIsUID, decl, ast_enum, Type::eResolveStateFull);
134 }
135 else if (auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type))
136 {
137 Type *target_type = m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId());
138 std::string name = type_def->getName();
139 uint64_t bytes = type_def->getLength();
140 if (!target_type)
141 return nullptr;
142 CompilerType target_ast_type = target_type->GetFullCompilerType();
143 CompilerDeclContext target_decl_ctx = m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID());
144 CompilerType ast_typedef = m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx);
145 return std::make_shared<Type>(type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes,
146 nullptr, target_type->GetID(), Type::eEncodingIsTypedefUID, decl, ast_typedef,
147 Type::eResolveStateFull);
148 }
149 else if (auto func_sig = llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type))
150 {
151 auto arg_enum = func_sig->getArguments();
152 uint32_t num_args = arg_enum->getChildCount();
153 std::vector<CompilerType> arg_list(num_args);
154 while (auto arg = arg_enum->getNext())
155 {
156 Type *arg_type = m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId());
157 // If there's some error looking up one of the dependent types of this function signature, bail.
158 if (!arg_type)
159 return nullptr;
160 CompilerType arg_ast_type = arg_type->GetFullCompilerType();
161 arg_list.push_back(arg_ast_type);
162 }
163 auto pdb_return_type = func_sig->getReturnType();
164 Type *return_type = m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId());
165 // If there's some error looking up one of the dependent types of this function signature, bail.
166 if (!return_type)
167 return nullptr;
168 CompilerType return_ast_type = return_type->GetFullCompilerType();
169 uint32_t type_quals = 0;
170 if (func_sig->isConstType())
171 type_quals |= clang::Qualifiers::Const;
172 if (func_sig->isVolatileType())
173 type_quals |= clang::Qualifiers::Volatile;
174 CompilerType func_sig_ast_type =
175 m_ast.CreateFunctionType(return_ast_type, &arg_list[0], num_args, false, type_quals);
176
177 return std::make_shared<Type>(func_sig->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 0, nullptr,
178 LLDB_INVALID_UID, Type::eEncodingIsUID, decl, func_sig_ast_type,
179 Type::eResolveStateFull);
180 }
181 else if (auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type))
182 {
183 uint32_t num_elements = array_type->getCount();
184 uint32_t element_uid = array_type->getElementType()->getSymIndexId();
185 uint32_t bytes = array_type->getLength();
186
187 Type *element_type = m_ast.GetSymbolFile()->ResolveTypeUID(element_uid);
188 CompilerType element_ast_type = element_type->GetFullCompilerType();
189 CompilerType array_ast_type = m_ast.CreateArrayType(element_ast_type, num_elements, false);
190 return std::make_shared<Type>(array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), bytes, nullptr,
191 LLDB_INVALID_UID, Type::eEncodingIsUID, decl, array_ast_type,
192 Type::eResolveStateFull);
193 }
194 return nullptr;
195}
196
197bool
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000198PDBASTParser::AddEnumValue(CompilerType enum_type, const PDBSymbolData &enum_value) const
Zachary Turner42dff792016-04-15 00:21:26 +0000199{
200 Declaration decl;
201 Variant v = enum_value.getValue();
202 std::string name = enum_value.getName();
203 int64_t raw_value;
204 switch (v.Type)
205 {
206 case PDB_VariantType::Int8:
207 raw_value = v.Value.Int8;
208 break;
209 case PDB_VariantType::Int16:
210 raw_value = v.Value.Int16;
211 break;
212 case PDB_VariantType::Int32:
213 raw_value = v.Value.Int32;
214 break;
215 case PDB_VariantType::Int64:
216 raw_value = v.Value.Int64;
217 break;
218 case PDB_VariantType::UInt8:
219 raw_value = v.Value.UInt8;
220 break;
221 case PDB_VariantType::UInt16:
222 raw_value = v.Value.UInt16;
223 break;
224 case PDB_VariantType::UInt32:
225 raw_value = v.Value.UInt32;
226 break;
227 case PDB_VariantType::UInt64:
228 raw_value = v.Value.UInt64;
229 break;
230 default:
231 return false;
232 }
233 CompilerType underlying_type = m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
234 uint32_t byte_size = m_ast.getASTContext()->getTypeSize(ClangUtil::GetQualType(underlying_type));
235 return m_ast.AddEnumerationValueToEnumerationType(enum_type.GetOpaqueQualType(), underlying_type, decl,
236 name.c_str(), raw_value, byte_size * 8);
237}