| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 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/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 | |
| 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
| 34 | using namespace llvm; |
| Zachary Turner | 54fd7ff | 2016-05-04 20:33:53 +0000 | [diff] [blame] | 35 | using namespace llvm::pdb; |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 36 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | int TranslateUdtKind(PDB_UdtType pdb_kind) { |
| 39 | switch (pdb_kind) { |
| 40 | case PDB_UdtType::Class: |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 41 | return clang::TTK_Class; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | 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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | lldb::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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {} |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 72 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | PDBASTParser::~PDBASTParser() {} |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 74 | |
| 75 | // DebugInfoASTParser interface |
| 76 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | lldb::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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 84 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | if (auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type)) { |
| 86 | AccessType access = lldb::eAccessPublic; |
| 87 | PDB_UdtType udt_kind = udt->getUdtKind(); |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 88 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | if (udt_kind == PDB_UdtType::Class) |
| 90 | access = lldb::eAccessPrivate; |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 91 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | 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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 95 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true); |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 97 | |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 98 | 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | } 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 Berghammer | 5976583 | 2017-11-07 10:39:22 +0000 | [diff] [blame] | 112 | name.c_str(), tu_decl_ctx, decl, builtin_type, false); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | 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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 118 | } |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 119 | |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 120 | 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | } else if (auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type)) { |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 125 | lldb_private::Type *target_type = |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | 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 Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 136 | return std::make_shared<lldb_private::Type>( |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 138 | bytes, nullptr, target_type->GetID(), |
| 139 | lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef, |
| 140 | lldb_private::Type::eResolveStateFull); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | } 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 Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 146 | lldb_private::Type *arg_type = |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | 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 Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 156 | lldb_private::Type *return_type = |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | 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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 170 | |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 171 | return std::make_shared<lldb_private::Type>( |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | func_sig->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 0, |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 173 | nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, |
| 174 | func_sig_ast_type, lldb_private::Type::eResolveStateFull); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | } 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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 179 | |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 180 | lldb_private::Type *element_type = |
| 181 | m_ast.GetSymbolFile()->ResolveTypeUID(element_uid); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | CompilerType element_ast_type = element_type->GetFullCompilerType(); |
| 183 | CompilerType array_ast_type = |
| 184 | m_ast.CreateArrayType(element_ast_type, num_elements, false); |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 185 | return std::make_shared<lldb_private::Type>( |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 187 | bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, |
| 188 | decl, array_ast_type, lldb_private::Type::eResolveStateFull); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | } |
| 190 | return nullptr; |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | bool 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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 234 | } |