| 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( | 
|  | 97 | ClangASTContext *clang_ast, const PDBSymbolTypeBuiltin *pdb_type, | 
|  | 98 | Encoding encoding, uint32_t width) { | 
|  | 99 | if (!pdb_type) | 
|  | 100 | return CompilerType(); | 
|  | 101 | if (!clang_ast) | 
|  | 102 | return CompilerType(); | 
|  | 103 | auto *ast = clang_ast->getASTContext(); | 
|  | 104 | if (!ast) | 
|  | 105 | return CompilerType(); | 
|  | 106 |  | 
|  | 107 | switch (pdb_type->getBuiltinType()) { | 
|  | 108 | default: break; | 
|  | 109 | case PDB_BuiltinType::None: | 
|  | 110 | return CompilerType(); | 
|  | 111 | case PDB_BuiltinType::Void: | 
| Adrian McCarthy | 1e368d1 | 2018-02-14 23:16:36 +0000 | [diff] [blame] | 112 | return clang_ast->GetBasicType(eBasicTypeVoid); | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 113 | case PDB_BuiltinType::Bool: | 
|  | 114 | return clang_ast->GetBasicType(eBasicTypeBool); | 
|  | 115 | case PDB_BuiltinType::Long: | 
|  | 116 | if (width == ast->getTypeSize(ast->LongTy)) | 
|  | 117 | return CompilerType(ast, ast->LongTy); | 
|  | 118 | if (width == ast->getTypeSize(ast->LongLongTy)) | 
|  | 119 | return CompilerType(ast, ast->LongLongTy); | 
|  | 120 | break; | 
|  | 121 | case PDB_BuiltinType::ULong: | 
|  | 122 | if (width == ast->getTypeSize(ast->UnsignedLongTy)) | 
|  | 123 | return CompilerType(ast, ast->UnsignedLongTy); | 
|  | 124 | if (width == ast->getTypeSize(ast->UnsignedLongLongTy)) | 
|  | 125 | return CompilerType(ast, ast->UnsignedLongLongTy); | 
|  | 126 | break; | 
|  | 127 | case PDB_BuiltinType::WCharT: | 
|  | 128 | if (width == ast->getTypeSize(ast->WCharTy)) | 
|  | 129 | return CompilerType(ast, ast->WCharTy); | 
|  | 130 | break; | 
| Aaron Smith | 2a989f3 | 2018-03-07 05:43:05 +0000 | [diff] [blame^] | 131 | case PDB_BuiltinType::Char16: | 
|  | 132 | return CompilerType(ast, ast->Char16Ty); | 
|  | 133 | case PDB_BuiltinType::Char32: | 
|  | 134 | return CompilerType(ast, ast->Char32Ty); | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 135 | case PDB_BuiltinType::Float: | 
|  | 136 | // Note: types `long double` and `double` have same bit size in MSVC and there | 
|  | 137 | // is no information in the PDB to distinguish them. So when falling back | 
|  | 138 | // to default search, the compiler type of `long double` will be represented by | 
|  | 139 | // the one generated for `double`. | 
|  | 140 | break; | 
|  | 141 | } | 
|  | 142 | // If there is no match on PDB_BuiltinType, fall back to default search | 
|  | 143 | // by encoding and width only | 
|  | 144 | return clang_ast->GetBuiltinTypeForEncodingAndBitSize(encoding, width); | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin *pdb_type, | 
|  | 148 | CompilerType &compiler_type) { | 
|  | 149 | if (!pdb_type) | 
|  | 150 | return compiler_type.GetTypeName(); | 
|  | 151 |  | 
|  | 152 | PDB_BuiltinType kind = pdb_type->getBuiltinType(); | 
|  | 153 | switch (kind) { | 
|  | 154 | default: break; | 
|  | 155 | case PDB_BuiltinType::Currency: | 
|  | 156 | return ConstString("CURRENCY"); | 
|  | 157 | case PDB_BuiltinType::Date: | 
|  | 158 | return ConstString("DATE"); | 
|  | 159 | case PDB_BuiltinType::Variant: | 
|  | 160 | return ConstString("VARIANT"); | 
|  | 161 | case PDB_BuiltinType::Complex: | 
|  | 162 | return ConstString("complex"); | 
|  | 163 | case PDB_BuiltinType::Bitfield: | 
|  | 164 | return ConstString("bitfield"); | 
|  | 165 | case PDB_BuiltinType::BSTR: | 
|  | 166 | return ConstString("BSTR"); | 
|  | 167 | case PDB_BuiltinType::HResult: | 
|  | 168 | return ConstString("HRESULT"); | 
|  | 169 | case PDB_BuiltinType::BCD: | 
|  | 170 | return ConstString("BCD"); | 
| Aaron Smith | 2a989f3 | 2018-03-07 05:43:05 +0000 | [diff] [blame^] | 171 | case PDB_BuiltinType::Char16: | 
|  | 172 | return ConstString("char16_t"); | 
|  | 173 | case PDB_BuiltinType::Char32: | 
|  | 174 | return ConstString("char32_t"); | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 175 | case PDB_BuiltinType::None: | 
|  | 176 | return ConstString("..."); | 
|  | 177 | } | 
|  | 178 | return compiler_type.GetTypeName(); | 
|  | 179 | } | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 180 |  | 
|  | 181 | bool GetDeclarationForSymbol(const PDBSymbol &symbol, Declaration &decl) { | 
|  | 182 | auto &raw_sym = symbol.getRawSymbol(); | 
| Aaron Smith | 7abdf2d | 2018-03-07 00:35:27 +0000 | [diff] [blame] | 183 | auto first_line_up = raw_sym.getSrcLineOnTypeDefn(); | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 184 |  | 
| Aaron Smith | 7abdf2d | 2018-03-07 00:35:27 +0000 | [diff] [blame] | 185 | if (!first_line_up) { | 
|  | 186 | auto lines_up = symbol.getSession().findLineNumbersByAddress( | 
|  | 187 | raw_sym.getVirtualAddress(), raw_sym.getLength()); | 
|  | 188 | if (!lines_up) | 
|  | 189 | return false; | 
|  | 190 | first_line_up = lines_up->getNext(); | 
|  | 191 | if (!first_line_up) | 
|  | 192 | return false; | 
|  | 193 | } | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 194 | uint32_t src_file_id = first_line_up->getSourceFileId(); | 
|  | 195 | auto src_file_up = symbol.getSession().getSourceFileById(src_file_id); | 
|  | 196 | if (!src_file_up) | 
|  | 197 | return false; | 
|  | 198 |  | 
|  | 199 | FileSpec spec(src_file_up->getFileName(), /*resolve_path*/false); | 
|  | 200 | decl.SetFile(spec); | 
|  | 201 | decl.SetColumn(first_line_up->getColumnNumber()); | 
|  | 202 | decl.SetLine(first_line_up->getLineNumber()); | 
|  | 203 | return true; | 
|  | 204 | } | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 205 | } | 
|  | 206 |  | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {} | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 208 |  | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | PDBASTParser::~PDBASTParser() {} | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 210 |  | 
|  | 211 | // DebugInfoASTParser interface | 
|  | 212 |  | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) { | 
|  | 214 | // PDB doesn't maintain enough information to robustly rebuild the entire | 
|  | 215 | // tree, and this is most problematic when it comes to figure out the | 
|  | 216 | // right DeclContext to put a type in.  So for now, everything goes in | 
|  | 217 | // the translation unit decl as a fully qualified type. | 
|  | 218 | clang::DeclContext *tu_decl_ctx = m_ast.GetTranslationUnitDecl(); | 
|  | 219 | Declaration decl; | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 220 |  | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 221 | switch (type.getSymTag()) { | 
|  | 222 | case PDB_SymType::UDT: { | 
|  | 223 | auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type); | 
|  | 224 | assert(udt); | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | AccessType access = lldb::eAccessPublic; | 
|  | 226 | PDB_UdtType udt_kind = udt->getUdtKind(); | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 227 | auto tag_type_kind = TranslateUdtKind(udt_kind); | 
|  | 228 | if (tag_type_kind == -1) | 
|  | 229 | return 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 | if (udt_kind == PDB_UdtType::Class) | 
|  | 232 | access = lldb::eAccessPrivate; | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 233 |  | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 234 | CompilerType clang_type = m_ast.CreateRecordType( | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 235 | tu_decl_ctx, access, udt->getName().c_str(), tag_type_kind, | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 236 | lldb::eLanguageTypeC_plus_plus, nullptr); | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 237 |  | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 238 | m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true); | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 239 |  | 
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 240 | return std::make_shared<lldb_private::Type>( | 
|  | 241 | type.getSymIndexId(), m_ast.GetSymbolFile(), | 
|  | 242 | ConstString(udt->getName()), udt->getLength(), nullptr, | 
|  | 243 | LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, clang_type, | 
|  | 244 | lldb_private::Type::eResolveStateForward); | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 245 | } break; | 
|  | 246 | case PDB_SymType::Enum: { | 
|  | 247 | auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type); | 
|  | 248 | assert(enum_type); | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 249 | auto underlying_type_up = enum_type->getUnderlyingType(); | 
|  | 250 | if (!underlying_type_up) | 
|  | 251 | return nullptr; | 
| Pavel Labath | 11d0b294 | 2018-01-22 11:51:56 +0000 | [diff] [blame] | 252 | lldb::Encoding encoding = | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 253 | TranslateBuiltinEncoding(underlying_type_up->getBuiltinType()); | 
|  | 254 | // FIXME: Type of underlying builtin is always `Int`. We correct it with | 
|  | 255 | // the very first enumerator's encoding if any. | 
|  | 256 | auto first_child = enum_type->findOneChild<PDBSymbolData>(); | 
|  | 257 | if (first_child) { | 
|  | 258 | encoding = TranslateEnumEncoding(first_child->getValue().Type); | 
|  | 259 | } | 
|  | 260 | std::string name = enum_type->getName(); | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 261 | uint64_t bytes = enum_type->getLength(); | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 262 | CompilerType builtin_type; | 
|  | 263 | if (bytes > 0) | 
|  | 264 | builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize( | 
|  | 265 | &m_ast, underlying_type_up.get(), encoding, bytes * 8); | 
|  | 266 | else | 
|  | 267 | builtin_type = m_ast.GetBasicType(eBasicTypeInt); | 
|  | 268 | // FIXME: PDB does not have information about scoped enumeration (Enum Class). | 
|  | 269 | // Set it false for now. | 
|  | 270 | bool isScoped = false; | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 271 |  | 
|  | 272 | CompilerType ast_enum = m_ast.CreateEnumerationType( | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 273 | name.c_str(), tu_decl_ctx, decl, builtin_type, isScoped); | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 274 | auto enum_values = enum_type->findAllChildren<PDBSymbolData>(); | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 275 | if (enum_values) { | 
|  | 276 | while (auto enum_value = enum_values->getNext()) { | 
|  | 277 | if (enum_value->getDataKind() != PDB_DataKind::Constant) | 
|  | 278 | continue; | 
|  | 279 | AddEnumValue(ast_enum, *enum_value); | 
|  | 280 | } | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 281 | } | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 282 | if (ClangASTContext::StartTagDeclarationDefinition(ast_enum)) | 
|  | 283 | ClangASTContext::CompleteTagDeclarationDefinition(ast_enum); | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 284 |  | 
| Aaron Smith | 7abdf2d | 2018-03-07 00:35:27 +0000 | [diff] [blame] | 285 | GetDeclarationForSymbol(type, decl); | 
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 286 | return std::make_shared<lldb_private::Type>( | 
|  | 287 | type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes, | 
|  | 288 | nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, | 
|  | 289 | ast_enum, lldb_private::Type::eResolveStateFull); | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 290 | } break; | 
|  | 291 | case PDB_SymType::Typedef: { | 
|  | 292 | auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type); | 
|  | 293 | assert(type_def); | 
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 294 | lldb_private::Type *target_type = | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 295 | m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId()); | 
| Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 296 | if (!target_type) | 
|  | 297 | return nullptr; | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 298 | std::string name = type_def->getName(); | 
|  | 299 | uint64_t bytes = type_def->getLength(); | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 300 | CompilerType target_ast_type = target_type->GetFullCompilerType(); | 
|  | 301 | CompilerDeclContext target_decl_ctx = | 
|  | 302 | m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID()); | 
|  | 303 | CompilerType ast_typedef = | 
|  | 304 | m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx); | 
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 305 | if (!ast_typedef) | 
|  | 306 | return nullptr; | 
|  | 307 |  | 
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 308 | return std::make_shared<lldb_private::Type>( | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 309 | type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), | 
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 310 | bytes, nullptr, target_type->GetID(), | 
|  | 311 | lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef, | 
|  | 312 | lldb_private::Type::eResolveStateFull); | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 313 | } break; | 
|  | 314 | case PDB_SymType::Function: | 
|  | 315 | case PDB_SymType::FunctionSig: { | 
|  | 316 | std::string name; | 
|  | 317 | PDBSymbolTypeFunctionSig *func_sig = nullptr; | 
|  | 318 | if (auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(&type)) { | 
|  | 319 | auto sig = pdb_func->getSignature(); | 
|  | 320 | if (!sig) | 
|  | 321 | return nullptr; | 
|  | 322 | func_sig = sig.release(); | 
|  | 323 | // Function type is named. | 
|  | 324 | name = pdb_func->getName(); | 
|  | 325 | } else if (auto pdb_func_sig = | 
|  | 326 | llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) { | 
|  | 327 | func_sig = const_cast<PDBSymbolTypeFunctionSig*>(pdb_func_sig); | 
|  | 328 | } else | 
|  | 329 | llvm_unreachable("Unexpected PDB symbol!"); | 
|  | 330 |  | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 331 | auto arg_enum = func_sig->getArguments(); | 
|  | 332 | uint32_t num_args = arg_enum->getChildCount(); | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 333 | std::vector<CompilerType> arg_list; | 
|  | 334 |  | 
|  | 335 | bool is_variadic = func_sig->isCVarArgs(); | 
|  | 336 | // Drop last variadic argument. | 
|  | 337 | if (is_variadic) | 
|  | 338 | --num_args; | 
| Kirill Bobyrev | de6fad6 | 2018-01-29 12:59:07 +0000 | [diff] [blame] | 339 | for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) { | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 340 | auto arg = arg_enum->getChildAtIndex(arg_idx); | 
|  | 341 | if (!arg) | 
|  | 342 | break; | 
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 343 | lldb_private::Type *arg_type = | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 344 | m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId()); | 
|  | 345 | // If there's some error looking up one of the dependent types of this | 
|  | 346 | // function signature, bail. | 
|  | 347 | if (!arg_type) | 
|  | 348 | return nullptr; | 
|  | 349 | CompilerType arg_ast_type = arg_type->GetFullCompilerType(); | 
|  | 350 | arg_list.push_back(arg_ast_type); | 
|  | 351 | } | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 352 | lldbassert(arg_list.size() <= num_args); | 
|  | 353 |  | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 354 | auto pdb_return_type = func_sig->getReturnType(); | 
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 355 | lldb_private::Type *return_type = | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 356 | m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId()); | 
|  | 357 | // If there's some error looking up one of the dependent types of this | 
|  | 358 | // function signature, bail. | 
|  | 359 | if (!return_type) | 
|  | 360 | return nullptr; | 
|  | 361 | CompilerType return_ast_type = return_type->GetFullCompilerType(); | 
|  | 362 | uint32_t type_quals = 0; | 
|  | 363 | if (func_sig->isConstType()) | 
|  | 364 | type_quals |= clang::Qualifiers::Const; | 
|  | 365 | if (func_sig->isVolatileType()) | 
|  | 366 | type_quals |= clang::Qualifiers::Volatile; | 
|  | 367 | CompilerType func_sig_ast_type = m_ast.CreateFunctionType( | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 368 | return_ast_type, arg_list.data(), arg_list.size(), is_variadic, | 
|  | 369 | type_quals); | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 370 |  | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 371 | GetDeclarationForSymbol(type, decl); | 
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 372 | return std::make_shared<lldb_private::Type>( | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 373 | type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 0, | 
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 374 | nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, | 
|  | 375 | func_sig_ast_type, lldb_private::Type::eResolveStateFull); | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 376 | } break; | 
|  | 377 | case PDB_SymType::ArrayType: { | 
|  | 378 | auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type); | 
|  | 379 | assert(array_type); | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 380 | uint32_t num_elements = array_type->getCount(); | 
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 381 | uint32_t element_uid = array_type->getElementTypeId(); | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 382 | uint32_t bytes = array_type->getLength(); | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 383 |  | 
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 384 | // If array rank > 0, PDB gives the element type at N=0. So element type | 
|  | 385 | // will parsed in the order N=0, N=1,..., N=rank sequentially. | 
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 386 | lldb_private::Type *element_type = | 
|  | 387 | m_ast.GetSymbolFile()->ResolveTypeUID(element_uid); | 
| Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 388 | if (!element_type) | 
|  | 389 | return nullptr; | 
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 390 |  | 
|  | 391 | CompilerType element_ast_type = element_type->GetForwardCompilerType(); | 
|  | 392 | // If element type is UDT, it needs to be complete. | 
|  | 393 | if (ClangASTContext::IsCXXClassType(element_ast_type) && | 
|  | 394 | element_ast_type.GetCompleteType() == false) { | 
|  | 395 | if (ClangASTContext::StartTagDeclarationDefinition(element_ast_type)) { | 
|  | 396 | ClangASTContext::CompleteTagDeclarationDefinition(element_ast_type); | 
|  | 397 | } else { | 
|  | 398 | // We are not able to start defintion. | 
|  | 399 | return nullptr; | 
|  | 400 | } | 
|  | 401 | } | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 402 | CompilerType array_ast_type = | 
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 403 | m_ast.CreateArrayType(element_ast_type, num_elements, /*is_gnu_vector*/false); | 
|  | 404 | TypeSP type_sp = std::make_shared<lldb_private::Type>( | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 405 | array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), | 
| Saleem Abdulrasool | 6c13510 | 2017-09-09 00:13:49 +0000 | [diff] [blame] | 406 | bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, | 
|  | 407 | decl, array_ast_type, lldb_private::Type::eResolveStateFull); | 
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 408 | type_sp->SetEncodingType(element_type); | 
|  | 409 | return type_sp; | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 410 | } break; | 
|  | 411 | case PDB_SymType::BuiltinType: { | 
|  | 412 | auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type); | 
|  | 413 | assert(builtin_type); | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 414 | PDB_BuiltinType builtin_kind = builtin_type->getBuiltinType(); | 
|  | 415 | if (builtin_kind == PDB_BuiltinType::None) | 
|  | 416 | return nullptr; | 
|  | 417 |  | 
|  | 418 | uint64_t bytes = builtin_type->getLength(); | 
|  | 419 | Encoding encoding = TranslateBuiltinEncoding(builtin_kind); | 
|  | 420 | CompilerType builtin_ast_type = GetBuiltinTypeForPDBEncodingAndBitSize( | 
|  | 421 | &m_ast, builtin_type, encoding, bytes * 8); | 
|  | 422 |  | 
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 423 | if (builtin_type->isConstType()) | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 424 | builtin_ast_type = builtin_ast_type.AddConstModifier(); | 
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 425 |  | 
|  | 426 | if (builtin_type->isVolatileType()) | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 427 | builtin_ast_type = builtin_ast_type.AddVolatileModifier(); | 
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 428 |  | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 429 | auto type_name = GetPDBBuiltinTypeName(builtin_type, builtin_ast_type); | 
|  | 430 |  | 
|  | 431 | return std::make_shared<lldb_private::Type>( | 
|  | 432 | builtin_type->getSymIndexId(), m_ast.GetSymbolFile(), type_name, | 
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 433 | bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 434 | decl, builtin_ast_type, lldb_private::Type::eResolveStateFull); | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 435 | } break; | 
|  | 436 | case PDB_SymType::PointerType: { | 
|  | 437 | auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type); | 
|  | 438 | assert(pointer_type); | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 439 | Type *pointee_type = m_ast.GetSymbolFile()->ResolveTypeUID( | 
|  | 440 | pointer_type->getPointeeType()->getSymIndexId()); | 
|  | 441 | if (!pointee_type) | 
|  | 442 | return nullptr; | 
|  | 443 |  | 
|  | 444 | CompilerType pointer_ast_type; | 
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 445 | pointer_ast_type = pointee_type->GetFullCompilerType(); | 
|  | 446 | if (pointer_type->isReference()) | 
|  | 447 | pointer_ast_type = pointer_ast_type.GetLValueReferenceType(); | 
|  | 448 | else if (pointer_type->getRawSymbol().isRValueReference()) | 
|  | 449 | pointer_ast_type = pointer_ast_type.GetRValueReferenceType(); | 
|  | 450 | else | 
|  | 451 | pointer_ast_type = pointer_ast_type.GetPointerType(); | 
|  | 452 |  | 
|  | 453 | if (pointer_type->isConstType()) | 
|  | 454 | pointer_ast_type = pointer_ast_type.AddConstModifier(); | 
|  | 455 |  | 
|  | 456 | if (pointer_type->isVolatileType()) | 
|  | 457 | pointer_ast_type = pointer_ast_type.AddVolatileModifier(); | 
|  | 458 |  | 
|  | 459 | if (pointer_type->getRawSymbol().isRestrictedType()) | 
|  | 460 | pointer_ast_type = pointer_ast_type.AddRestrictModifier(); | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 461 |  | 
|  | 462 | return std::make_shared<lldb_private::Type>( | 
|  | 463 | pointer_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), | 
|  | 464 | pointer_type->getLength(), nullptr, LLDB_INVALID_UID, | 
| Aaron Smith | a0db2eb | 2018-03-07 00:39:25 +0000 | [diff] [blame] | 465 | lldb_private::Type::eEncodingIsUID, decl, pointer_ast_type, | 
| Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 466 | lldb_private::Type::eResolveStateFull); | 
| Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 467 | } break; | 
|  | 468 | default: break; | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 469 | } | 
|  | 470 | return nullptr; | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 471 | } | 
|  | 472 |  | 
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 473 | bool PDBASTParser::AddEnumValue(CompilerType enum_type, | 
|  | 474 | const PDBSymbolData &enum_value) const { | 
|  | 475 | Declaration decl; | 
|  | 476 | Variant v = enum_value.getValue(); | 
|  | 477 | std::string name = enum_value.getName(); | 
|  | 478 | int64_t raw_value; | 
|  | 479 | switch (v.Type) { | 
|  | 480 | case PDB_VariantType::Int8: | 
|  | 481 | raw_value = v.Value.Int8; | 
|  | 482 | break; | 
|  | 483 | case PDB_VariantType::Int16: | 
|  | 484 | raw_value = v.Value.Int16; | 
|  | 485 | break; | 
|  | 486 | case PDB_VariantType::Int32: | 
|  | 487 | raw_value = v.Value.Int32; | 
|  | 488 | break; | 
|  | 489 | case PDB_VariantType::Int64: | 
|  | 490 | raw_value = v.Value.Int64; | 
|  | 491 | break; | 
|  | 492 | case PDB_VariantType::UInt8: | 
|  | 493 | raw_value = v.Value.UInt8; | 
|  | 494 | break; | 
|  | 495 | case PDB_VariantType::UInt16: | 
|  | 496 | raw_value = v.Value.UInt16; | 
|  | 497 | break; | 
|  | 498 | case PDB_VariantType::UInt32: | 
|  | 499 | raw_value = v.Value.UInt32; | 
|  | 500 | break; | 
|  | 501 | case PDB_VariantType::UInt64: | 
|  | 502 | raw_value = v.Value.UInt64; | 
|  | 503 | break; | 
|  | 504 | default: | 
|  | 505 | return false; | 
|  | 506 | } | 
|  | 507 | CompilerType underlying_type = | 
|  | 508 | m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType()); | 
|  | 509 | uint32_t byte_size = m_ast.getASTContext()->getTypeSize( | 
|  | 510 | ClangUtil::GetQualType(underlying_type)); | 
|  | 511 | return m_ast.AddEnumerationValueToEnumerationType( | 
|  | 512 | enum_type.GetOpaqueQualType(), underlying_type, decl, name.c_str(), | 
|  | 513 | raw_value, byte_size * 8); | 
| Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 514 | } |