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/IPDBLineNumber.h" 23 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" 24 #include "llvm/DebugInfo/PDB/PDBSymbol.h" 25 #include "llvm/DebugInfo/PDB/PDBSymbolData.h" 26 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" 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" 32 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h" 33 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" 34 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" 35 36 using namespace lldb; 37 using namespace lldb_private; 38 using namespace llvm::pdb; 39 40 namespace { 41 int TranslateUdtKind(PDB_UdtType pdb_kind) { 42 switch (pdb_kind) { 43 case PDB_UdtType::Class: 44 return clang::TTK_Class; 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 } 52 return -1; 53 } 54 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: 62 return lldb::eEncodingSint; 63 case PDB_BuiltinType::Bool: 64 case PDB_BuiltinType::UInt: 65 case PDB_BuiltinType::ULong: 66 case PDB_BuiltinType::HResult: 67 return lldb::eEncodingUint; 68 default: 69 return lldb::eEncodingInvalid; 70 } 71 } 72 73 lldb::Encoding TranslateEnumEncoding(PDB_VariantType type) { 74 switch (type) { 75 case PDB_VariantType::Int8: 76 case PDB_VariantType::Int16: 77 case PDB_VariantType::Int32: 78 case PDB_VariantType::Int64: 79 return lldb::eEncodingSint; 80 81 case PDB_VariantType::UInt8: 82 case PDB_VariantType::UInt16: 83 case PDB_VariantType::UInt32: 84 case PDB_VariantType::UInt64: 85 return lldb::eEncodingUint; 86 87 default: 88 break; 89 } 90 91 return lldb::eEncodingSint; 92 } 93 94 CompilerType GetBuiltinTypeForPDBEncodingAndBitSize( 95 ClangASTContext *clang_ast, const PDBSymbolTypeBuiltin *pdb_type, 96 Encoding encoding, uint32_t width) { 97 if (!pdb_type) 98 return CompilerType(); 99 if (!clang_ast) 100 return CompilerType(); 101 auto *ast = clang_ast->getASTContext(); 102 if (!ast) 103 return CompilerType(); 104 105 switch (pdb_type->getBuiltinType()) { 106 default: break; 107 case PDB_BuiltinType::None: 108 return CompilerType(); 109 case PDB_BuiltinType::Void: 110 return clang_ast->GetBasicType(eBasicTypeVoid); 111 case PDB_BuiltinType::Bool: 112 return clang_ast->GetBasicType(eBasicTypeBool); 113 case PDB_BuiltinType::Long: 114 if (width == ast->getTypeSize(ast->LongTy)) 115 return CompilerType(ast, ast->LongTy); 116 if (width == ast->getTypeSize(ast->LongLongTy)) 117 return CompilerType(ast, ast->LongLongTy); 118 break; 119 case PDB_BuiltinType::ULong: 120 if (width == ast->getTypeSize(ast->UnsignedLongTy)) 121 return CompilerType(ast, ast->UnsignedLongTy); 122 if (width == ast->getTypeSize(ast->UnsignedLongLongTy)) 123 return CompilerType(ast, ast->UnsignedLongLongTy); 124 break; 125 case PDB_BuiltinType::WCharT: 126 if (width == ast->getTypeSize(ast->WCharTy)) 127 return CompilerType(ast, ast->WCharTy); 128 break; 129 case PDB_BuiltinType::Float: 130 // Note: types `long double` and `double` have same bit size in MSVC and there 131 // is no information in the PDB to distinguish them. So when falling back 132 // to default search, the compiler type of `long double` will be represented by 133 // the one generated for `double`. 134 break; 135 } 136 // If there is no match on PDB_BuiltinType, fall back to default search 137 // by encoding and width only 138 return clang_ast->GetBuiltinTypeForEncodingAndBitSize(encoding, width); 139 } 140 141 ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin *pdb_type, 142 CompilerType &compiler_type) { 143 if (!pdb_type) 144 return compiler_type.GetTypeName(); 145 146 PDB_BuiltinType kind = pdb_type->getBuiltinType(); 147 switch (kind) { 148 default: break; 149 case PDB_BuiltinType::Currency: 150 return ConstString("CURRENCY"); 151 case PDB_BuiltinType::Date: 152 return ConstString("DATE"); 153 case PDB_BuiltinType::Variant: 154 return ConstString("VARIANT"); 155 case PDB_BuiltinType::Complex: 156 return ConstString("complex"); 157 case PDB_BuiltinType::Bitfield: 158 return ConstString("bitfield"); 159 case PDB_BuiltinType::BSTR: 160 return ConstString("BSTR"); 161 case PDB_BuiltinType::HResult: 162 return ConstString("HRESULT"); 163 case PDB_BuiltinType::BCD: 164 return ConstString("BCD"); 165 case PDB_BuiltinType::None: 166 return ConstString("..."); 167 } 168 return compiler_type.GetTypeName(); 169 } 170 171 bool GetDeclarationForSymbol(const PDBSymbol &symbol, Declaration &decl) { 172 auto &raw_sym = symbol.getRawSymbol(); 173 auto lines_up = symbol.getSession().findLineNumbersByAddress( 174 raw_sym.getVirtualAddress(), raw_sym.getLength()); 175 if (!lines_up) 176 return false; 177 auto first_line_up = lines_up->getNext(); 178 if (!first_line_up) 179 return false; 180 181 uint32_t src_file_id = first_line_up->getSourceFileId(); 182 auto src_file_up = symbol.getSession().getSourceFileById(src_file_id); 183 if (!src_file_up) 184 return false; 185 186 FileSpec spec(src_file_up->getFileName(), /*resolve_path*/false); 187 decl.SetFile(spec); 188 decl.SetColumn(first_line_up->getColumnNumber()); 189 decl.SetLine(first_line_up->getLineNumber()); 190 return true; 191 } 192 } 193 194 PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {} 195 196 PDBASTParser::~PDBASTParser() {} 197 198 // DebugInfoASTParser interface 199 200 lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) { 201 // PDB doesn't maintain enough information to robustly rebuild the entire 202 // tree, and this is most problematic when it comes to figure out the 203 // right DeclContext to put a type in. So for now, everything goes in 204 // the translation unit decl as a fully qualified type. 205 clang::DeclContext *tu_decl_ctx = m_ast.GetTranslationUnitDecl(); 206 Declaration decl; 207 208 switch (type.getSymTag()) { 209 case PDB_SymType::UDT: { 210 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type); 211 assert(udt); 212 AccessType access = lldb::eAccessPublic; 213 PDB_UdtType udt_kind = udt->getUdtKind(); 214 auto tag_type_kind = TranslateUdtKind(udt_kind); 215 if (tag_type_kind == -1) 216 return nullptr; 217 218 if (udt_kind == PDB_UdtType::Class) 219 access = lldb::eAccessPrivate; 220 221 CompilerType clang_type = m_ast.CreateRecordType( 222 tu_decl_ctx, access, udt->getName().c_str(), tag_type_kind, 223 lldb::eLanguageTypeC_plus_plus, nullptr); 224 225 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true); 226 227 return std::make_shared<lldb_private::Type>( 228 type.getSymIndexId(), m_ast.GetSymbolFile(), 229 ConstString(udt->getName()), udt->getLength(), nullptr, 230 LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, clang_type, 231 lldb_private::Type::eResolveStateForward); 232 } break; 233 case PDB_SymType::Enum: { 234 auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type); 235 assert(enum_type); 236 auto underlying_type_up = enum_type->getUnderlyingType(); 237 if (!underlying_type_up) 238 return nullptr; 239 lldb::Encoding encoding = 240 TranslateBuiltinEncoding(underlying_type_up->getBuiltinType()); 241 // FIXME: Type of underlying builtin is always `Int`. We correct it with 242 // the very first enumerator's encoding if any. 243 auto first_child = enum_type->findOneChild<PDBSymbolData>(); 244 if (first_child) { 245 encoding = TranslateEnumEncoding(first_child->getValue().Type); 246 } 247 std::string name = enum_type->getName(); 248 uint64_t bytes = enum_type->getLength(); 249 CompilerType builtin_type; 250 if (bytes > 0) 251 builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize( 252 &m_ast, underlying_type_up.get(), encoding, bytes * 8); 253 else 254 builtin_type = m_ast.GetBasicType(eBasicTypeInt); 255 // FIXME: PDB does not have information about scoped enumeration (Enum Class). 256 // Set it false for now. 257 bool isScoped = false; 258 259 CompilerType ast_enum = m_ast.CreateEnumerationType( 260 name.c_str(), tu_decl_ctx, decl, builtin_type, isScoped); 261 auto enum_values = enum_type->findAllChildren<PDBSymbolData>(); 262 if (enum_values) { 263 while (auto enum_value = enum_values->getNext()) { 264 if (enum_value->getDataKind() != PDB_DataKind::Constant) 265 continue; 266 AddEnumValue(ast_enum, *enum_value); 267 } 268 } 269 if (ClangASTContext::StartTagDeclarationDefinition(ast_enum)) 270 ClangASTContext::CompleteTagDeclarationDefinition(ast_enum); 271 272 return std::make_shared<lldb_private::Type>( 273 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes, 274 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 275 ast_enum, lldb_private::Type::eResolveStateFull); 276 } break; 277 case PDB_SymType::Typedef: { 278 auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type); 279 assert(type_def); 280 lldb_private::Type *target_type = 281 m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId()); 282 if (!target_type) 283 return nullptr; 284 std::string name = type_def->getName(); 285 uint64_t bytes = type_def->getLength(); 286 CompilerType target_ast_type = target_type->GetFullCompilerType(); 287 CompilerDeclContext target_decl_ctx = 288 m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID()); 289 CompilerType ast_typedef = 290 m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx); 291 return std::make_shared<lldb_private::Type>( 292 type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 293 bytes, nullptr, target_type->GetID(), 294 lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef, 295 lldb_private::Type::eResolveStateFull); 296 } break; 297 case PDB_SymType::Function: 298 case PDB_SymType::FunctionSig: { 299 std::string name; 300 PDBSymbolTypeFunctionSig *func_sig = nullptr; 301 if (auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(&type)) { 302 auto sig = pdb_func->getSignature(); 303 if (!sig) 304 return nullptr; 305 func_sig = sig.release(); 306 // Function type is named. 307 name = pdb_func->getName(); 308 } else if (auto pdb_func_sig = 309 llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) { 310 func_sig = const_cast<PDBSymbolTypeFunctionSig*>(pdb_func_sig); 311 } else 312 llvm_unreachable("Unexpected PDB symbol!"); 313 314 auto arg_enum = func_sig->getArguments(); 315 uint32_t num_args = arg_enum->getChildCount(); 316 std::vector<CompilerType> arg_list; 317 318 bool is_variadic = func_sig->isCVarArgs(); 319 // Drop last variadic argument. 320 if (is_variadic) 321 --num_args; 322 for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) { 323 auto arg = arg_enum->getChildAtIndex(arg_idx); 324 if (!arg) 325 break; 326 lldb_private::Type *arg_type = 327 m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId()); 328 // If there's some error looking up one of the dependent types of this 329 // function signature, bail. 330 if (!arg_type) 331 return nullptr; 332 CompilerType arg_ast_type = arg_type->GetFullCompilerType(); 333 arg_list.push_back(arg_ast_type); 334 } 335 lldbassert(arg_list.size() <= num_args); 336 337 auto pdb_return_type = func_sig->getReturnType(); 338 lldb_private::Type *return_type = 339 m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId()); 340 // If there's some error looking up one of the dependent types of this 341 // function signature, bail. 342 if (!return_type) 343 return nullptr; 344 CompilerType return_ast_type = return_type->GetFullCompilerType(); 345 uint32_t type_quals = 0; 346 if (func_sig->isConstType()) 347 type_quals |= clang::Qualifiers::Const; 348 if (func_sig->isVolatileType()) 349 type_quals |= clang::Qualifiers::Volatile; 350 CompilerType func_sig_ast_type = m_ast.CreateFunctionType( 351 return_ast_type, arg_list.data(), arg_list.size(), is_variadic, 352 type_quals); 353 354 GetDeclarationForSymbol(type, decl); 355 return std::make_shared<lldb_private::Type>( 356 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 0, 357 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 358 func_sig_ast_type, lldb_private::Type::eResolveStateFull); 359 } break; 360 case PDB_SymType::ArrayType: { 361 auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type); 362 assert(array_type); 363 uint32_t num_elements = array_type->getCount(); 364 uint32_t element_uid = array_type->getElementType()->getSymIndexId(); 365 uint32_t bytes = array_type->getLength(); 366 367 lldb_private::Type *element_type = 368 m_ast.GetSymbolFile()->ResolveTypeUID(element_uid); 369 if (!element_type) 370 return nullptr; 371 CompilerType element_ast_type = element_type->GetFullCompilerType(); 372 CompilerType array_ast_type = 373 m_ast.CreateArrayType(element_ast_type, num_elements, false); 374 return std::make_shared<lldb_private::Type>( 375 array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 376 bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, 377 decl, array_ast_type, lldb_private::Type::eResolveStateFull); 378 } break; 379 case PDB_SymType::BuiltinType: { 380 auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type); 381 assert(builtin_type); 382 PDB_BuiltinType builtin_kind = builtin_type->getBuiltinType(); 383 if (builtin_kind == PDB_BuiltinType::None) 384 return nullptr; 385 386 uint64_t bytes = builtin_type->getLength(); 387 Encoding encoding = TranslateBuiltinEncoding(builtin_kind); 388 CompilerType builtin_ast_type = GetBuiltinTypeForPDBEncodingAndBitSize( 389 &m_ast, builtin_type, encoding, bytes * 8); 390 391 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID; 392 if (builtin_type->isConstType()) { 393 encoding_data_type = Type::eEncodingIsConstUID; 394 builtin_ast_type = builtin_ast_type.AddConstModifier(); 395 } 396 if (builtin_type->isVolatileType()) { 397 encoding_data_type = Type::eEncodingIsVolatileUID; 398 builtin_ast_type = builtin_ast_type.AddVolatileModifier(); 399 } 400 auto type_name = GetPDBBuiltinTypeName(builtin_type, builtin_ast_type); 401 402 return std::make_shared<lldb_private::Type>( 403 builtin_type->getSymIndexId(), m_ast.GetSymbolFile(), type_name, 404 bytes, nullptr, LLDB_INVALID_UID, encoding_data_type, 405 decl, builtin_ast_type, lldb_private::Type::eResolveStateFull); 406 } break; 407 case PDB_SymType::PointerType: { 408 auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type); 409 assert(pointer_type); 410 Type *pointee_type = m_ast.GetSymbolFile()->ResolveTypeUID( 411 pointer_type->getPointeeType()->getSymIndexId()); 412 if (!pointee_type) 413 return nullptr; 414 415 CompilerType pointer_ast_type; 416 Type::EncodingDataType encoding_data_type = Type::eEncodingIsPointerUID; 417 if (pointer_type->isReference()) { 418 encoding_data_type = Type::eEncodingIsLValueReferenceUID; 419 pointer_ast_type = 420 pointee_type->GetFullCompilerType().GetLValueReferenceType(); 421 } else 422 pointer_ast_type = pointee_type->GetFullCompilerType().GetPointerType(); 423 424 return std::make_shared<lldb_private::Type>( 425 pointer_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 426 pointer_type->getLength(), nullptr, LLDB_INVALID_UID, 427 encoding_data_type, decl, pointer_ast_type, 428 lldb_private::Type::eResolveStateFull); 429 } break; 430 default: break; 431 } 432 return nullptr; 433 } 434 435 bool PDBASTParser::AddEnumValue(CompilerType enum_type, 436 const PDBSymbolData &enum_value) const { 437 Declaration decl; 438 Variant v = enum_value.getValue(); 439 std::string name = enum_value.getName(); 440 int64_t raw_value; 441 switch (v.Type) { 442 case PDB_VariantType::Int8: 443 raw_value = v.Value.Int8; 444 break; 445 case PDB_VariantType::Int16: 446 raw_value = v.Value.Int16; 447 break; 448 case PDB_VariantType::Int32: 449 raw_value = v.Value.Int32; 450 break; 451 case PDB_VariantType::Int64: 452 raw_value = v.Value.Int64; 453 break; 454 case PDB_VariantType::UInt8: 455 raw_value = v.Value.UInt8; 456 break; 457 case PDB_VariantType::UInt16: 458 raw_value = v.Value.UInt16; 459 break; 460 case PDB_VariantType::UInt32: 461 raw_value = v.Value.UInt32; 462 break; 463 case PDB_VariantType::UInt64: 464 raw_value = v.Value.UInt64; 465 break; 466 default: 467 return false; 468 } 469 CompilerType underlying_type = 470 m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType()); 471 uint32_t byte_size = m_ast.getASTContext()->getTypeSize( 472 ClangUtil::GetQualType(underlying_type)); 473 return m_ast.AddEnumerationValueToEnumerationType( 474 enum_type.GetOpaqueQualType(), underlying_type, decl, name.c_str(), 475 raw_value, byte_size * 8); 476 } 477