blob: e26d46df46b2dcdc48470a3a112eb3348d2b264b [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
Aaron Smith7ac1c782018-02-09 05:31:28 +000022#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
23#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Zachary Turner42dff792016-04-15 00:21:26 +000024#include "llvm/DebugInfo/PDB/PDBSymbol.h"
25#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000026#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
Zachary Turner42dff792016-04-15 00:21:26 +000027#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"
Aaron Smithec40f812018-01-23 20:35:19 +000032#include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
Zachary Turner42dff792016-04-15 00:21:26 +000033#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
34#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
35
36using namespace lldb;
37using namespace lldb_private;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000038using namespace llvm::pdb;
Zachary Turner42dff792016-04-15 00:21:26 +000039
Kate Stoneb9c1b512016-09-06 20:57:50 +000040namespace {
41int TranslateUdtKind(PDB_UdtType pdb_kind) {
42 switch (pdb_kind) {
43 case PDB_UdtType::Class:
Zachary Turner42dff792016-04-15 00:21:26 +000044 return clang::TTK_Class;
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 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 }
Aaron Smithec40f812018-01-23 20:35:19 +000052 return -1;
Zachary Turner42dff792016-04-15 00:21:26 +000053}
54
Kate Stoneb9c1b512016-09-06 20:57:50 +000055lldb::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:
Aaron Smith2a989f32018-03-07 05:43:05 +000062 case PDB_BuiltinType::Char16:
63 case PDB_BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 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 }
Zachary Turner42dff792016-04-15 00:21:26 +000073}
Aaron Smithec40f812018-01-23 20:35:19 +000074
75lldb::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
96CompilerType GetBuiltinTypeForPDBEncodingAndBitSize(
Aaron Smithe664b5d2018-03-19 21:14:19 +000097 ClangASTContext &clang_ast, const PDBSymbolTypeBuiltin &pdb_type,
Aaron Smithec40f812018-01-23 20:35:19 +000098 Encoding encoding, uint32_t width) {
Aaron Smithe664b5d2018-03-19 21:14:19 +000099 auto *ast = clang_ast.getASTContext();
Aaron Smithec40f812018-01-23 20:35:19 +0000100 if (!ast)
101 return CompilerType();
102
Aaron Smithe664b5d2018-03-19 21:14:19 +0000103 switch (pdb_type.getBuiltinType()) {
Aaron Smithec40f812018-01-23 20:35:19 +0000104 default: break;
105 case PDB_BuiltinType::None:
106 return CompilerType();
107 case PDB_BuiltinType::Void:
Aaron Smithe664b5d2018-03-19 21:14:19 +0000108 return clang_ast.GetBasicType(eBasicTypeVoid);
Aaron Smithec40f812018-01-23 20:35:19 +0000109 case PDB_BuiltinType::Bool:
Aaron Smithe664b5d2018-03-19 21:14:19 +0000110 return clang_ast.GetBasicType(eBasicTypeBool);
Aaron Smithec40f812018-01-23 20:35:19 +0000111 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;
Aaron Smith2a989f32018-03-07 05:43:05 +0000127 case PDB_BuiltinType::Char16:
128 return CompilerType(ast, ast->Char16Ty);
129 case PDB_BuiltinType::Char32:
130 return CompilerType(ast, ast->Char32Ty);
Aaron Smithec40f812018-01-23 20:35:19 +0000131 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
Aaron Smithe664b5d2018-03-19 21:14:19 +0000140 return clang_ast.GetBuiltinTypeForEncodingAndBitSize(encoding, width);
Aaron Smithec40f812018-01-23 20:35:19 +0000141}
142
Aaron Smithe664b5d2018-03-19 21:14:19 +0000143ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin &pdb_type,
Aaron Smithec40f812018-01-23 20:35:19 +0000144 CompilerType &compiler_type) {
Aaron Smithe664b5d2018-03-19 21:14:19 +0000145 PDB_BuiltinType kind = pdb_type.getBuiltinType();
Aaron Smithec40f812018-01-23 20:35:19 +0000146 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");
Aaron Smith2a989f32018-03-07 05:43:05 +0000164 case PDB_BuiltinType::Char16:
165 return ConstString("char16_t");
166 case PDB_BuiltinType::Char32:
167 return ConstString("char32_t");
Aaron Smithec40f812018-01-23 20:35:19 +0000168 case PDB_BuiltinType::None:
169 return ConstString("...");
170 }
171 return compiler_type.GetTypeName();
172}
Aaron Smith7ac1c782018-02-09 05:31:28 +0000173
174bool GetDeclarationForSymbol(const PDBSymbol &symbol, Declaration &decl) {
175 auto &raw_sym = symbol.getRawSymbol();
Aaron Smith7abdf2d2018-03-07 00:35:27 +0000176 auto first_line_up = raw_sym.getSrcLineOnTypeDefn();
Aaron Smith7ac1c782018-02-09 05:31:28 +0000177
Aaron Smith7abdf2d2018-03-07 00:35:27 +0000178 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 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000187 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}
Zachary Turner42dff792016-04-15 00:21:26 +0000198}
199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {}
Zachary Turner42dff792016-04-15 00:21:26 +0000201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202PDBASTParser::~PDBASTParser() {}
Zachary Turner42dff792016-04-15 00:21:26 +0000203
204// DebugInfoASTParser interface
205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206lldb::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;
Zachary Turner42dff792016-04-15 00:21:26 +0000213
Aaron Smith7ac1c782018-02-09 05:31:28 +0000214 switch (type.getSymTag()) {
215 case PDB_SymType::UDT: {
216 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type);
217 assert(udt);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 AccessType access = lldb::eAccessPublic;
219 PDB_UdtType udt_kind = udt->getUdtKind();
Aaron Smithec40f812018-01-23 20:35:19 +0000220 auto tag_type_kind = TranslateUdtKind(udt_kind);
221 if (tag_type_kind == -1)
222 return nullptr;
Zachary Turner42dff792016-04-15 00:21:26 +0000223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 if (udt_kind == PDB_UdtType::Class)
225 access = lldb::eAccessPrivate;
Zachary Turner42dff792016-04-15 00:21:26 +0000226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 CompilerType clang_type = m_ast.CreateRecordType(
Aaron Smithec40f812018-01-23 20:35:19 +0000228 tu_decl_ctx, access, udt->getName().c_str(), tag_type_kind,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 lldb::eLanguageTypeC_plus_plus, nullptr);
Zachary Turner42dff792016-04-15 00:21:26 +0000230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true);
Zachary Turner42dff792016-04-15 00:21:26 +0000232
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000233 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);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000238 } break;
239 case PDB_SymType::Enum: {
240 auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type);
241 assert(enum_type);
Aaron Smithec40f812018-01-23 20:35:19 +0000242 auto underlying_type_up = enum_type->getUnderlyingType();
243 if (!underlying_type_up)
244 return nullptr;
Pavel Labath11d0b2942018-01-22 11:51:56 +0000245 lldb::Encoding encoding =
Aaron Smithec40f812018-01-23 20:35:19 +0000246 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();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 uint64_t bytes = enum_type->getLength();
Aaron Smithec40f812018-01-23 20:35:19 +0000255 CompilerType builtin_type;
256 if (bytes > 0)
257 builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize(
Aaron Smithe664b5d2018-03-19 21:14:19 +0000258 m_ast, *underlying_type_up, encoding, bytes * 8);
Aaron Smithec40f812018-01-23 20:35:19 +0000259 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;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264
265 CompilerType ast_enum = m_ast.CreateEnumerationType(
Aaron Smithec40f812018-01-23 20:35:19 +0000266 name.c_str(), tu_decl_ctx, decl, builtin_type, isScoped);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 auto enum_values = enum_type->findAllChildren<PDBSymbolData>();
Aaron Smithec40f812018-01-23 20:35:19 +0000268 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 }
Zachary Turner42dff792016-04-15 00:21:26 +0000274 }
Aaron Smithec40f812018-01-23 20:35:19 +0000275 if (ClangASTContext::StartTagDeclarationDefinition(ast_enum))
276 ClangASTContext::CompleteTagDeclarationDefinition(ast_enum);
Zachary Turner42dff792016-04-15 00:21:26 +0000277
Aaron Smith7abdf2d2018-03-07 00:35:27 +0000278 GetDeclarationForSymbol(type, decl);
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000279 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);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000283 } break;
284 case PDB_SymType::Typedef: {
285 auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type);
286 assert(type_def);
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000287 lldb_private::Type *target_type =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId());
Aaron Smith86e94342017-12-22 05:26:50 +0000289 if (!target_type)
290 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 std::string name = type_def->getName();
292 uint64_t bytes = type_def->getLength();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293 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);
Aaron Smitha0db2eb2018-03-07 00:39:25 +0000298 if (!ast_typedef)
299 return nullptr;
300
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000301 return std::make_shared<lldb_private::Type>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name),
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000303 bytes, nullptr, target_type->GetID(),
304 lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef,
305 lldb_private::Type::eResolveStateFull);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000306 } 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 auto arg_enum = func_sig->getArguments();
325 uint32_t num_args = arg_enum->getChildCount();
Aaron Smithec40f812018-01-23 20:35:19 +0000326 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;
Kirill Bobyrevde6fad62018-01-29 12:59:07 +0000332 for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) {
Aaron Smithec40f812018-01-23 20:35:19 +0000333 auto arg = arg_enum->getChildAtIndex(arg_idx);
334 if (!arg)
335 break;
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000336 lldb_private::Type *arg_type =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337 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 }
Aaron Smithec40f812018-01-23 20:35:19 +0000345 lldbassert(arg_list.size() <= num_args);
346
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 auto pdb_return_type = func_sig->getReturnType();
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000348 lldb_private::Type *return_type =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 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(
Aaron Smithec40f812018-01-23 20:35:19 +0000361 return_ast_type, arg_list.data(), arg_list.size(), is_variadic,
362 type_quals);
Zachary Turner42dff792016-04-15 00:21:26 +0000363
Aaron Smith7ac1c782018-02-09 05:31:28 +0000364 GetDeclarationForSymbol(type, decl);
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000365 return std::make_shared<lldb_private::Type>(
Aaron Smith7ac1c782018-02-09 05:31:28 +0000366 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 0,
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000367 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl,
368 func_sig_ast_type, lldb_private::Type::eResolveStateFull);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000369 } break;
370 case PDB_SymType::ArrayType: {
371 auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type);
372 assert(array_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373 uint32_t num_elements = array_type->getCount();
Aaron Smitha0db2eb2018-03-07 00:39:25 +0000374 uint32_t element_uid = array_type->getElementTypeId();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 uint32_t bytes = array_type->getLength();
Zachary Turner42dff792016-04-15 00:21:26 +0000376
Aaron Smitha0db2eb2018-03-07 00:39:25 +0000377 // 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.
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000379 lldb_private::Type *element_type =
380 m_ast.GetSymbolFile()->ResolveTypeUID(element_uid);
Aaron Smith86e94342017-12-22 05:26:50 +0000381 if (!element_type)
382 return nullptr;
Aaron Smitha0db2eb2018-03-07 00:39:25 +0000383
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 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 CompilerType array_ast_type =
Aaron Smitha0db2eb2018-03-07 00:39:25 +0000396 m_ast.CreateArrayType(element_ast_type, num_elements, /*is_gnu_vector*/false);
397 TypeSP type_sp = std::make_shared<lldb_private::Type>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(),
Saleem Abdulrasool6c135102017-09-09 00:13:49 +0000399 bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID,
400 decl, array_ast_type, lldb_private::Type::eResolveStateFull);
Aaron Smitha0db2eb2018-03-07 00:39:25 +0000401 type_sp->SetEncodingType(element_type);
402 return type_sp;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000403 } break;
404 case PDB_SymType::BuiltinType: {
405 auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type);
406 assert(builtin_type);
Aaron Smithec40f812018-01-23 20:35:19 +0000407 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(
Aaron Smithe664b5d2018-03-19 21:14:19 +0000414 m_ast, *builtin_type, encoding, bytes * 8);
Aaron Smithec40f812018-01-23 20:35:19 +0000415
Aaron Smitha0db2eb2018-03-07 00:39:25 +0000416 if (builtin_type->isConstType())
Aaron Smithec40f812018-01-23 20:35:19 +0000417 builtin_ast_type = builtin_ast_type.AddConstModifier();
Aaron Smitha0db2eb2018-03-07 00:39:25 +0000418
419 if (builtin_type->isVolatileType())
Aaron Smithec40f812018-01-23 20:35:19 +0000420 builtin_ast_type = builtin_ast_type.AddVolatileModifier();
Aaron Smitha0db2eb2018-03-07 00:39:25 +0000421
Aaron Smithe664b5d2018-03-19 21:14:19 +0000422 auto type_name = GetPDBBuiltinTypeName(*builtin_type, builtin_ast_type);
Aaron Smithec40f812018-01-23 20:35:19 +0000423
424 return std::make_shared<lldb_private::Type>(
425 builtin_type->getSymIndexId(), m_ast.GetSymbolFile(), type_name,
Aaron Smitha0db2eb2018-03-07 00:39:25 +0000426 bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID,
Aaron Smithec40f812018-01-23 20:35:19 +0000427 decl, builtin_ast_type, lldb_private::Type::eResolveStateFull);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000428 } break;
429 case PDB_SymType::PointerType: {
430 auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type);
431 assert(pointer_type);
Aaron Smithec40f812018-01-23 20:35:19 +0000432 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;
Aaron Smitha0db2eb2018-03-07 00:39:25 +0000438 pointer_ast_type = pointee_type->GetFullCompilerType();
439 if (pointer_type->isReference())
440 pointer_ast_type = pointer_ast_type.GetLValueReferenceType();
441 else if (pointer_type->getRawSymbol().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->getRawSymbol().isRestrictedType())
453 pointer_ast_type = pointer_ast_type.AddRestrictModifier();
Aaron Smithec40f812018-01-23 20:35:19 +0000454
455 return std::make_shared<lldb_private::Type>(
456 pointer_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(),
457 pointer_type->getLength(), nullptr, LLDB_INVALID_UID,
Aaron Smitha0db2eb2018-03-07 00:39:25 +0000458 lldb_private::Type::eEncodingIsUID, decl, pointer_ast_type,
Aaron Smithec40f812018-01-23 20:35:19 +0000459 lldb_private::Type::eResolveStateFull);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000460 } break;
461 default: break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 }
463 return nullptr;
Zachary Turner42dff792016-04-15 00:21:26 +0000464}
465
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466bool 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);
Zachary Turner42dff792016-04-15 00:21:26 +0000507}