| 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 | |
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" |
| 23 | #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/PDB/PDBSymbol.h" |
| 25 | #include "llvm/DebugInfo/PDB/PDBSymbolData.h" |
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 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" |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 32 | #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h" |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 33 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" |
| 34 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" |
| 35 | |
| 36 | using namespace lldb; |
| 37 | using namespace lldb_private; |
| Zachary Turner | 54fd7ff | 2016-05-04 20:33:53 +0000 | [diff] [blame] | 38 | using namespace llvm::pdb; |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 39 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | int TranslateUdtKind(PDB_UdtType pdb_kind) { |
| 42 | switch (pdb_kind) { |
| 43 | case PDB_UdtType::Class: |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 44 | return clang::TTK_Class; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 | } |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 52 | return -1; |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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: |
| Aaron Smith | 2a989f3 | 2018-03-07 05:43:05 +0000 | [diff] [blame] | 62 | case PDB_BuiltinType::Char16: |
| 63 | case PDB_BuiltinType::Char32: |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | 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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 73 | } |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 74 | |
| 75 | lldb::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 | |
| 96 | CompilerType GetBuiltinTypeForPDBEncodingAndBitSize( |
| Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame^] | 97 | ClangASTContext &clang_ast, const PDBSymbolTypeBuiltin &pdb_type, |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 98 | Encoding encoding, uint32_t width) { |
| Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame^] | 99 | auto *ast = clang_ast.getASTContext(); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 100 | if (!ast) |
| 101 | return CompilerType(); |
| 102 | |
| Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame^] | 103 | switch (pdb_type.getBuiltinType()) { |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 104 | default: break; |
| 105 | case PDB_BuiltinType::None: |
| 106 | return CompilerType(); |
| 107 | case PDB_BuiltinType::Void: |
| Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame^] | 108 | return clang_ast.GetBasicType(eBasicTypeVoid); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 109 | case PDB_BuiltinType::Bool: |
| Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame^] | 110 | return clang_ast.GetBasicType(eBasicTypeBool); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 111 | 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 Smith | 2a989f3 | 2018-03-07 05:43:05 +0000 | [diff] [blame] | 127 | case PDB_BuiltinType::Char16: |
| 128 | return CompilerType(ast, ast->Char16Ty); |
| 129 | case PDB_BuiltinType::Char32: |
| 130 | return CompilerType(ast, ast->Char32Ty); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 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 |
| Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame^] | 140 | return clang_ast.GetBuiltinTypeForEncodingAndBitSize(encoding, width); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame^] | 143 | ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin &pdb_type, |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 144 | CompilerType &compiler_type) { |
| Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame^] | 145 | PDB_BuiltinType kind = pdb_type.getBuiltinType(); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 146 | 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 Smith | 2a989f3 | 2018-03-07 05:43:05 +0000 | [diff] [blame] | 164 | case PDB_BuiltinType::Char16: |
| 165 | return ConstString("char16_t"); |
| 166 | case PDB_BuiltinType::Char32: |
| 167 | return ConstString("char32_t"); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 168 | case PDB_BuiltinType::None: |
| 169 | return ConstString("..."); |
| 170 | } |
| 171 | return compiler_type.GetTypeName(); |
| 172 | } |
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 173 | |
| 174 | bool GetDeclarationForSymbol(const PDBSymbol &symbol, Declaration &decl) { |
| 175 | auto &raw_sym = symbol.getRawSymbol(); |
| Aaron Smith | 7abdf2d | 2018-03-07 00:35:27 +0000 | [diff] [blame] | 176 | auto first_line_up = raw_sym.getSrcLineOnTypeDefn(); |
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 177 | |
| Aaron Smith | 7abdf2d | 2018-03-07 00:35:27 +0000 | [diff] [blame] | 178 | 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 Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 187 | 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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {} |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 201 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | PDBASTParser::~PDBASTParser() {} |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 203 | |
| 204 | // DebugInfoASTParser interface |
| 205 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 206 | lldb::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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 213 | |
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 214 | switch (type.getSymTag()) { |
| 215 | case PDB_SymType::UDT: { |
| 216 | auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type); |
| 217 | assert(udt); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 218 | AccessType access = lldb::eAccessPublic; |
| 219 | PDB_UdtType udt_kind = udt->getUdtKind(); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 220 | auto tag_type_kind = TranslateUdtKind(udt_kind); |
| 221 | if (tag_type_kind == -1) |
| 222 | return nullptr; |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 223 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | if (udt_kind == PDB_UdtType::Class) |
| 225 | access = lldb::eAccessPrivate; |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 226 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 227 | CompilerType clang_type = m_ast.CreateRecordType( |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 228 | tu_decl_ctx, access, udt->getName().c_str(), tag_type_kind, |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | lldb::eLanguageTypeC_plus_plus, nullptr); |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 230 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 231 | m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true); |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 232 | |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 233 | 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 Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 238 | } break; |
| 239 | case PDB_SymType::Enum: { |
| 240 | auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type); |
| 241 | assert(enum_type); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 242 | auto underlying_type_up = enum_type->getUnderlyingType(); |
| 243 | if (!underlying_type_up) |
| 244 | return nullptr; |
| Pavel Labath | 11d0b294 | 2018-01-22 11:51:56 +0000 | [diff] [blame] | 245 | lldb::Encoding encoding = |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 246 | 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 254 | uint64_t bytes = enum_type->getLength(); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 255 | CompilerType builtin_type; |
| 256 | if (bytes > 0) |
| 257 | builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize( |
| Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame^] | 258 | m_ast, *underlying_type_up, encoding, bytes * 8); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 259 | 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 264 | |
| 265 | CompilerType ast_enum = m_ast.CreateEnumerationType( |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 266 | name.c_str(), tu_decl_ctx, decl, builtin_type, isScoped); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 267 | auto enum_values = enum_type->findAllChildren<PDBSymbolData>(); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 268 | 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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 274 | } |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 275 | if (ClangASTContext::StartTagDeclarationDefinition(ast_enum)) |
| 276 | ClangASTContext::CompleteTagDeclarationDefinition(ast_enum); |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 277 | |
| Aaron Smith | 7abdf2d | 2018-03-07 00:35:27 +0000 | [diff] [blame] | 278 | GetDeclarationForSymbol(type, decl); |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 279 | 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 Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 283 | } break; |
| 284 | case PDB_SymType::Typedef: { |
| 285 | auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type); |
| 286 | assert(type_def); |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 287 | lldb_private::Type *target_type = |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 288 | m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId()); |
| Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 289 | if (!target_type) |
| 290 | return nullptr; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 291 | std::string name = type_def->getName(); |
| 292 | uint64_t bytes = type_def->getLength(); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 293 | 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 Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 298 | if (!ast_typedef) |
| 299 | return nullptr; |
| 300 | |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 301 | return std::make_shared<lldb_private::Type>( |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 302 | type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 303 | bytes, nullptr, target_type->GetID(), |
| 304 | lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef, |
| 305 | lldb_private::Type::eResolveStateFull); |
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 306 | } 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 324 | auto arg_enum = func_sig->getArguments(); |
| 325 | uint32_t num_args = arg_enum->getChildCount(); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 326 | 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 Bobyrev | de6fad6 | 2018-01-29 12:59:07 +0000 | [diff] [blame] | 332 | for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) { |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 333 | auto arg = arg_enum->getChildAtIndex(arg_idx); |
| 334 | if (!arg) |
| 335 | break; |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 336 | lldb_private::Type *arg_type = |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 337 | 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 Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 345 | lldbassert(arg_list.size() <= num_args); |
| 346 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 347 | auto pdb_return_type = func_sig->getReturnType(); |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 348 | lldb_private::Type *return_type = |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 349 | 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 Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 361 | return_ast_type, arg_list.data(), arg_list.size(), is_variadic, |
| 362 | type_quals); |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 363 | |
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 364 | GetDeclarationForSymbol(type, decl); |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 365 | return std::make_shared<lldb_private::Type>( |
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 366 | type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 0, |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 367 | nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, |
| 368 | func_sig_ast_type, lldb_private::Type::eResolveStateFull); |
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 369 | } break; |
| 370 | case PDB_SymType::ArrayType: { |
| 371 | auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type); |
| 372 | assert(array_type); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 373 | uint32_t num_elements = array_type->getCount(); |
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 374 | uint32_t element_uid = array_type->getElementTypeId(); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 375 | uint32_t bytes = array_type->getLength(); |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 376 | |
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 377 | // 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 Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 379 | lldb_private::Type *element_type = |
| 380 | m_ast.GetSymbolFile()->ResolveTypeUID(element_uid); |
| Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 381 | if (!element_type) |
| 382 | return nullptr; |
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 383 | |
| 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 395 | CompilerType array_ast_type = |
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 396 | m_ast.CreateArrayType(element_ast_type, num_elements, /*is_gnu_vector*/false); |
| 397 | TypeSP type_sp = std::make_shared<lldb_private::Type>( |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 398 | array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), |
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 399 | bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, |
| 400 | decl, array_ast_type, lldb_private::Type::eResolveStateFull); |
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 401 | type_sp->SetEncodingType(element_type); |
| 402 | return type_sp; |
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 403 | } break; |
| 404 | case PDB_SymType::BuiltinType: { |
| 405 | auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type); |
| 406 | assert(builtin_type); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 407 | 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 Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame^] | 414 | m_ast, *builtin_type, encoding, bytes * 8); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 415 | |
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 416 | if (builtin_type->isConstType()) |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 417 | builtin_ast_type = builtin_ast_type.AddConstModifier(); |
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 418 | |
| 419 | if (builtin_type->isVolatileType()) |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 420 | builtin_ast_type = builtin_ast_type.AddVolatileModifier(); |
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 421 | |
| Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame^] | 422 | auto type_name = GetPDBBuiltinTypeName(*builtin_type, builtin_ast_type); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 423 | |
| 424 | return std::make_shared<lldb_private::Type>( |
| 425 | builtin_type->getSymIndexId(), m_ast.GetSymbolFile(), type_name, |
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 426 | bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 427 | decl, builtin_ast_type, lldb_private::Type::eResolveStateFull); |
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 428 | } break; |
| 429 | case PDB_SymType::PointerType: { |
| 430 | auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type); |
| 431 | assert(pointer_type); |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 432 | 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 Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 438 | 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 Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 454 | |
| 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 Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 458 | lldb_private::Type::eEncodingIsUID, decl, pointer_ast_type, |
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 459 | lldb_private::Type::eResolveStateFull); |
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 460 | } break; |
| 461 | default: break; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 462 | } |
| 463 | return nullptr; |
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 466 | bool 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 Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 507 | } |