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::Char16: 65 case PDB_BuiltinType::Char32: 66 case PDB_BuiltinType::UInt: 67 case PDB_BuiltinType::ULong: 68 case PDB_BuiltinType::HResult: 69 case PDB_BuiltinType::WCharT: 70 return lldb::eEncodingUint; 71 default: 72 return lldb::eEncodingInvalid; 73 } 74 } 75 76 lldb::Encoding TranslateEnumEncoding(PDB_VariantType type) { 77 switch (type) { 78 case PDB_VariantType::Int8: 79 case PDB_VariantType::Int16: 80 case PDB_VariantType::Int32: 81 case PDB_VariantType::Int64: 82 return lldb::eEncodingSint; 83 84 case PDB_VariantType::UInt8: 85 case PDB_VariantType::UInt16: 86 case PDB_VariantType::UInt32: 87 case PDB_VariantType::UInt64: 88 return lldb::eEncodingUint; 89 90 default: 91 break; 92 } 93 94 return lldb::eEncodingSint; 95 } 96 97 CompilerType 98 GetBuiltinTypeForPDBEncodingAndBitSize(ClangASTContext &clang_ast, 99 const PDBSymbolTypeBuiltin &pdb_type, 100 Encoding encoding, uint32_t width) { 101 auto *ast = clang_ast.getASTContext(); 102 if (!ast) 103 return CompilerType(); 104 105 switch (pdb_type.getBuiltinType()) { 106 default: 107 break; 108 case PDB_BuiltinType::None: 109 return CompilerType(); 110 case PDB_BuiltinType::Void: 111 return clang_ast.GetBasicType(eBasicTypeVoid); 112 case PDB_BuiltinType::Bool: 113 return clang_ast.GetBasicType(eBasicTypeBool); 114 case PDB_BuiltinType::Long: 115 if (width == ast->getTypeSize(ast->LongTy)) 116 return CompilerType(ast, ast->LongTy); 117 if (width == ast->getTypeSize(ast->LongLongTy)) 118 return CompilerType(ast, ast->LongLongTy); 119 break; 120 case PDB_BuiltinType::ULong: 121 if (width == ast->getTypeSize(ast->UnsignedLongTy)) 122 return CompilerType(ast, ast->UnsignedLongTy); 123 if (width == ast->getTypeSize(ast->UnsignedLongLongTy)) 124 return CompilerType(ast, ast->UnsignedLongLongTy); 125 break; 126 case PDB_BuiltinType::WCharT: 127 if (width == ast->getTypeSize(ast->WCharTy)) 128 return CompilerType(ast, ast->WCharTy); 129 break; 130 case PDB_BuiltinType::Char16: 131 return CompilerType(ast, ast->Char16Ty); 132 case PDB_BuiltinType::Char32: 133 return CompilerType(ast, ast->Char32Ty); 134 case PDB_BuiltinType::Float: 135 // Note: types `long double` and `double` have same bit size in MSVC and 136 // there is no information in the PDB to distinguish them. So when falling 137 // back to default search, the compiler type of `long double` will be 138 // represented by the one generated for `double`. 139 break; 140 } 141 // If there is no match on PDB_BuiltinType, fall back to default search by 142 // encoding and width only 143 return clang_ast.GetBuiltinTypeForEncodingAndBitSize(encoding, width); 144 } 145 146 ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin &pdb_type, 147 CompilerType &compiler_type) { 148 PDB_BuiltinType kind = pdb_type.getBuiltinType(); 149 switch (kind) { 150 default: 151 break; 152 case PDB_BuiltinType::Currency: 153 return ConstString("CURRENCY"); 154 case PDB_BuiltinType::Date: 155 return ConstString("DATE"); 156 case PDB_BuiltinType::Variant: 157 return ConstString("VARIANT"); 158 case PDB_BuiltinType::Complex: 159 return ConstString("complex"); 160 case PDB_BuiltinType::Bitfield: 161 return ConstString("bitfield"); 162 case PDB_BuiltinType::BSTR: 163 return ConstString("BSTR"); 164 case PDB_BuiltinType::HResult: 165 return ConstString("HRESULT"); 166 case PDB_BuiltinType::BCD: 167 return ConstString("BCD"); 168 case PDB_BuiltinType::Char16: 169 return ConstString("char16_t"); 170 case PDB_BuiltinType::Char32: 171 return ConstString("char32_t"); 172 case PDB_BuiltinType::None: 173 return ConstString("..."); 174 } 175 return compiler_type.GetTypeName(); 176 } 177 178 bool GetDeclarationForSymbol(const PDBSymbol &symbol, Declaration &decl) { 179 auto &raw_sym = symbol.getRawSymbol(); 180 auto first_line_up = raw_sym.getSrcLineOnTypeDefn(); 181 182 if (!first_line_up) { 183 auto lines_up = symbol.getSession().findLineNumbersByAddress( 184 raw_sym.getVirtualAddress(), raw_sym.getLength()); 185 if (!lines_up) 186 return false; 187 first_line_up = lines_up->getNext(); 188 if (!first_line_up) 189 return false; 190 } 191 uint32_t src_file_id = first_line_up->getSourceFileId(); 192 auto src_file_up = symbol.getSession().getSourceFileById(src_file_id); 193 if (!src_file_up) 194 return false; 195 196 FileSpec spec(src_file_up->getFileName(), /*resolve_path*/ false); 197 decl.SetFile(spec); 198 decl.SetColumn(first_line_up->getColumnNumber()); 199 decl.SetLine(first_line_up->getLineNumber()); 200 return true; 201 } 202 } // namespace 203 204 PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {} 205 206 PDBASTParser::~PDBASTParser() {} 207 208 // DebugInfoASTParser interface 209 210 lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) { 211 // PDB doesn't maintain enough information to robustly rebuild the entire 212 // tree, and this is most problematic when it comes to figure out the right 213 // DeclContext to put a type in. So for now, everything goes in the 214 // translation unit decl as a fully qualified type. 215 clang::DeclContext *tu_decl_ctx = m_ast.GetTranslationUnitDecl(); 216 Declaration decl; 217 218 switch (type.getSymTag()) { 219 case PDB_SymType::UDT: { 220 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type); 221 assert(udt); 222 AccessType access = lldb::eAccessPublic; 223 PDB_UdtType udt_kind = udt->getUdtKind(); 224 auto tag_type_kind = TranslateUdtKind(udt_kind); 225 if (tag_type_kind == -1) 226 return nullptr; 227 228 if (udt_kind == PDB_UdtType::Class) 229 access = lldb::eAccessPrivate; 230 231 CompilerType clang_type = m_ast.CreateRecordType( 232 tu_decl_ctx, access, udt->getName().c_str(), tag_type_kind, 233 lldb::eLanguageTypeC_plus_plus, nullptr); 234 235 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true); 236 237 return std::make_shared<lldb_private::Type>( 238 type.getSymIndexId(), m_ast.GetSymbolFile(), 239 ConstString(udt->getName()), udt->getLength(), nullptr, 240 LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, clang_type, 241 lldb_private::Type::eResolveStateForward); 242 } break; 243 case PDB_SymType::Enum: { 244 auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type); 245 assert(enum_type); 246 auto underlying_type_up = enum_type->getUnderlyingType(); 247 if (!underlying_type_up) 248 return nullptr; 249 lldb::Encoding encoding = 250 TranslateBuiltinEncoding(underlying_type_up->getBuiltinType()); 251 // FIXME: Type of underlying builtin is always `Int`. We correct it with 252 // the very first enumerator's encoding if any. 253 auto first_child = enum_type->findOneChild<PDBSymbolData>(); 254 if (first_child) { 255 encoding = TranslateEnumEncoding(first_child->getValue().Type); 256 } 257 std::string name = enum_type->getName(); 258 uint64_t bytes = enum_type->getLength(); 259 CompilerType builtin_type; 260 if (bytes > 0) 261 builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize( 262 m_ast, *underlying_type_up, encoding, bytes * 8); 263 else 264 builtin_type = m_ast.GetBasicType(eBasicTypeInt); 265 // FIXME: PDB does not have information about scoped enumeration (Enum 266 // Class). Set it false for now. 267 bool isScoped = false; 268 269 CompilerType ast_enum = m_ast.CreateEnumerationType( 270 name.c_str(), tu_decl_ctx, decl, builtin_type, isScoped); 271 auto enum_values = enum_type->findAllChildren<PDBSymbolData>(); 272 if (enum_values) { 273 while (auto enum_value = enum_values->getNext()) { 274 if (enum_value->getDataKind() != PDB_DataKind::Constant) 275 continue; 276 AddEnumValue(ast_enum, *enum_value); 277 } 278 } 279 if (ClangASTContext::StartTagDeclarationDefinition(ast_enum)) 280 ClangASTContext::CompleteTagDeclarationDefinition(ast_enum); 281 282 GetDeclarationForSymbol(type, decl); 283 return std::make_shared<lldb_private::Type>( 284 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes, 285 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 286 ast_enum, lldb_private::Type::eResolveStateFull); 287 } break; 288 case PDB_SymType::Typedef: { 289 auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type); 290 assert(type_def); 291 lldb_private::Type *target_type = 292 m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId()); 293 if (!target_type) 294 return nullptr; 295 std::string name = type_def->getName(); 296 uint64_t bytes = type_def->getLength(); 297 CompilerType target_ast_type = target_type->GetFullCompilerType(); 298 CompilerDeclContext target_decl_ctx = 299 m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID()); 300 CompilerType ast_typedef = 301 m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx); 302 if (!ast_typedef) 303 return nullptr; 304 305 return std::make_shared<lldb_private::Type>( 306 type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 307 bytes, nullptr, target_type->GetID(), 308 lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef, 309 lldb_private::Type::eResolveStateFull); 310 } break; 311 case PDB_SymType::Function: 312 case PDB_SymType::FunctionSig: { 313 std::string name; 314 PDBSymbolTypeFunctionSig *func_sig = nullptr; 315 if (auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(&type)) { 316 if (pdb_func->isCompilerGenerated()) 317 return nullptr; 318 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 331 auto arg_enum = func_sig->getArguments(); 332 uint32_t num_args = arg_enum->getChildCount(); 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; 339 for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) { 340 auto arg = arg_enum->getChildAtIndex(arg_idx); 341 if (!arg) 342 break; 343 lldb_private::Type *arg_type = 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 } 352 lldbassert(arg_list.size() <= num_args); 353 354 auto pdb_return_type = func_sig->getReturnType(); 355 lldb_private::Type *return_type = 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 = 368 m_ast.CreateFunctionType(return_ast_type, arg_list.data(), 369 arg_list.size(), is_variadic, type_quals); 370 371 GetDeclarationForSymbol(type, decl); 372 return std::make_shared<lldb_private::Type>( 373 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 0, 374 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 375 func_sig_ast_type, lldb_private::Type::eResolveStateFull); 376 } break; 377 case PDB_SymType::ArrayType: { 378 auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type); 379 assert(array_type); 380 uint32_t num_elements = array_type->getCount(); 381 uint32_t element_uid = array_type->getElementTypeId(); 382 uint32_t bytes = array_type->getLength(); 383 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. 386 lldb_private::Type *element_type = 387 m_ast.GetSymbolFile()->ResolveTypeUID(element_uid); 388 if (!element_type) 389 return nullptr; 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 } 402 CompilerType array_ast_type = m_ast.CreateArrayType( 403 element_ast_type, num_elements, /*is_gnu_vector*/ false); 404 TypeSP type_sp = std::make_shared<lldb_private::Type>( 405 array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 406 bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, 407 decl, array_ast_type, lldb_private::Type::eResolveStateFull); 408 type_sp->SetEncodingType(element_type); 409 return type_sp; 410 } break; 411 case PDB_SymType::BuiltinType: { 412 auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type); 413 assert(builtin_type); 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 423 if (builtin_type->isConstType()) 424 builtin_ast_type = builtin_ast_type.AddConstModifier(); 425 426 if (builtin_type->isVolatileType()) 427 builtin_ast_type = builtin_ast_type.AddVolatileModifier(); 428 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, bytes, 433 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 434 builtin_ast_type, lldb_private::Type::eResolveStateFull); 435 } break; 436 case PDB_SymType::PointerType: { 437 auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type); 438 assert(pointer_type); 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; 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->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->isRestrictedType()) 460 pointer_ast_type = pointer_ast_type.AddRestrictModifier(); 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, 465 lldb_private::Type::eEncodingIsUID, decl, pointer_ast_type, 466 lldb_private::Type::eResolveStateFull); 467 } break; 468 default: 469 break; 470 } 471 return nullptr; 472 } 473 474 bool PDBASTParser::AddEnumValue(CompilerType enum_type, 475 const PDBSymbolData &enum_value) const { 476 Declaration decl; 477 Variant v = enum_value.getValue(); 478 std::string name = enum_value.getName(); 479 int64_t raw_value; 480 switch (v.Type) { 481 case PDB_VariantType::Int8: 482 raw_value = v.Value.Int8; 483 break; 484 case PDB_VariantType::Int16: 485 raw_value = v.Value.Int16; 486 break; 487 case PDB_VariantType::Int32: 488 raw_value = v.Value.Int32; 489 break; 490 case PDB_VariantType::Int64: 491 raw_value = v.Value.Int64; 492 break; 493 case PDB_VariantType::UInt8: 494 raw_value = v.Value.UInt8; 495 break; 496 case PDB_VariantType::UInt16: 497 raw_value = v.Value.UInt16; 498 break; 499 case PDB_VariantType::UInt32: 500 raw_value = v.Value.UInt32; 501 break; 502 case PDB_VariantType::UInt64: 503 raw_value = v.Value.UInt64; 504 break; 505 default: 506 return false; 507 } 508 CompilerType underlying_type = 509 m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType()); 510 uint32_t byte_size = m_ast.getASTContext()->getTypeSize( 511 ClangUtil::GetQualType(underlying_type)); 512 return m_ast.AddEnumerationValueToEnumerationType( 513 enum_type.GetOpaqueQualType(), underlying_type, decl, name.c_str(), 514 raw_value, byte_size * 8); 515 } 516