1 //===-- PDBASTParser.cpp ----------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "PDBASTParser.h" 11 12 #include "clang/AST/CharUnits.h" 13 #include "clang/AST/Decl.h" 14 #include "clang/AST/DeclCXX.h" 15 16 #include "lldb/Symbol/ClangASTContext.h" 17 #include "lldb/Symbol/ClangUtil.h" 18 #include "lldb/Symbol/Declaration.h" 19 #include "lldb/Symbol/SymbolFile.h" 20 #include "lldb/Symbol/TypeSystem.h" 21 22 #include "llvm/DebugInfo/PDB/PDBSymbol.h" 23 #include "llvm/DebugInfo/PDB/PDBSymbolData.h" 24 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h" 25 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h" 26 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" 27 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h" 28 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h" 29 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h" 30 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" 31 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" 32 33 using namespace lldb; 34 using namespace lldb_private; 35 using namespace llvm::pdb; 36 37 namespace { 38 int TranslateUdtKind(PDB_UdtType pdb_kind) { 39 switch (pdb_kind) { 40 case PDB_UdtType::Class: 41 return clang::TTK_Class; 42 case PDB_UdtType::Struct: 43 return clang::TTK_Struct; 44 case PDB_UdtType::Union: 45 return clang::TTK_Union; 46 case PDB_UdtType::Interface: 47 return clang::TTK_Interface; 48 } 49 return -1; 50 } 51 52 lldb::Encoding TranslateBuiltinEncoding(PDB_BuiltinType type) { 53 switch (type) { 54 case PDB_BuiltinType::Float: 55 return lldb::eEncodingIEEE754; 56 case PDB_BuiltinType::Int: 57 case PDB_BuiltinType::Long: 58 case PDB_BuiltinType::Char: 59 return lldb::eEncodingSint; 60 case PDB_BuiltinType::Bool: 61 case PDB_BuiltinType::UInt: 62 case PDB_BuiltinType::ULong: 63 case PDB_BuiltinType::HResult: 64 return lldb::eEncodingUint; 65 default: 66 return lldb::eEncodingInvalid; 67 } 68 } 69 70 lldb::Encoding TranslateEnumEncoding(PDB_VariantType type) { 71 switch (type) { 72 case PDB_VariantType::Int8: 73 case PDB_VariantType::Int16: 74 case PDB_VariantType::Int32: 75 case PDB_VariantType::Int64: 76 return lldb::eEncodingSint; 77 78 case PDB_VariantType::UInt8: 79 case PDB_VariantType::UInt16: 80 case PDB_VariantType::UInt32: 81 case PDB_VariantType::UInt64: 82 return lldb::eEncodingUint; 83 84 default: 85 break; 86 } 87 88 return lldb::eEncodingSint; 89 } 90 91 CompilerType GetBuiltinTypeForPDBEncodingAndBitSize( 92 ClangASTContext *clang_ast, const PDBSymbolTypeBuiltin *pdb_type, 93 Encoding encoding, uint32_t width) { 94 if (!pdb_type) 95 return CompilerType(); 96 if (!clang_ast) 97 return CompilerType(); 98 auto *ast = clang_ast->getASTContext(); 99 if (!ast) 100 return CompilerType(); 101 102 switch (pdb_type->getBuiltinType()) { 103 default: break; 104 case PDB_BuiltinType::None: 105 return CompilerType(); 106 case PDB_BuiltinType::Void: 107 // FIXME: where is non-zero size of `void` from? 108 if (width == 0) 109 return clang_ast->GetBasicType(eBasicTypeVoid); 110 case PDB_BuiltinType::Bool: 111 return clang_ast->GetBasicType(eBasicTypeBool); 112 case PDB_BuiltinType::Long: 113 if (width == ast->getTypeSize(ast->LongTy)) 114 return CompilerType(ast, ast->LongTy); 115 if (width == ast->getTypeSize(ast->LongLongTy)) 116 return CompilerType(ast, ast->LongLongTy); 117 break; 118 case PDB_BuiltinType::ULong: 119 if (width == ast->getTypeSize(ast->UnsignedLongTy)) 120 return CompilerType(ast, ast->UnsignedLongTy); 121 if (width == ast->getTypeSize(ast->UnsignedLongLongTy)) 122 return CompilerType(ast, ast->UnsignedLongLongTy); 123 break; 124 case PDB_BuiltinType::WCharT: 125 if (width == ast->getTypeSize(ast->WCharTy)) 126 return CompilerType(ast, ast->WCharTy); 127 break; 128 case PDB_BuiltinType::Float: 129 // Note: types `long double` and `double` have same bit size in MSVC and there 130 // is no information in the PDB to distinguish them. So when falling back 131 // to default search, the compiler type of `long double` will be represented by 132 // the one generated for `double`. 133 break; 134 } 135 // If there is no match on PDB_BuiltinType, fall back to default search 136 // by encoding and width only 137 return clang_ast->GetBuiltinTypeForEncodingAndBitSize(encoding, width); 138 } 139 140 ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin *pdb_type, 141 CompilerType &compiler_type) { 142 if (!pdb_type) 143 return compiler_type.GetTypeName(); 144 145 PDB_BuiltinType kind = pdb_type->getBuiltinType(); 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"); 164 case PDB_BuiltinType::None: 165 return ConstString("..."); 166 } 167 return compiler_type.GetTypeName(); 168 } 169 } 170 171 PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {} 172 173 PDBASTParser::~PDBASTParser() {} 174 175 // DebugInfoASTParser interface 176 177 lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) { 178 // PDB doesn't maintain enough information to robustly rebuild the entire 179 // tree, and this is most problematic when it comes to figure out the 180 // right DeclContext to put a type in. So for now, everything goes in 181 // the translation unit decl as a fully qualified type. 182 clang::DeclContext *tu_decl_ctx = m_ast.GetTranslationUnitDecl(); 183 Declaration decl; 184 185 if (auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type)) { 186 AccessType access = lldb::eAccessPublic; 187 PDB_UdtType udt_kind = udt->getUdtKind(); 188 auto tag_type_kind = TranslateUdtKind(udt_kind); 189 if (tag_type_kind == -1) 190 return nullptr; 191 192 if (udt_kind == PDB_UdtType::Class) 193 access = lldb::eAccessPrivate; 194 195 CompilerType clang_type = m_ast.CreateRecordType( 196 tu_decl_ctx, access, udt->getName().c_str(), tag_type_kind, 197 lldb::eLanguageTypeC_plus_plus, nullptr); 198 199 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true); 200 201 return std::make_shared<lldb_private::Type>( 202 type.getSymIndexId(), m_ast.GetSymbolFile(), 203 ConstString(udt->getName()), udt->getLength(), nullptr, 204 LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, clang_type, 205 lldb_private::Type::eResolveStateForward); 206 } else if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type)) { 207 auto underlying_type_up = enum_type->getUnderlyingType(); 208 if (!underlying_type_up) 209 return nullptr; 210 lldb::Encoding encoding = 211 TranslateBuiltinEncoding(underlying_type_up->getBuiltinType()); 212 // FIXME: Type of underlying builtin is always `Int`. We correct it with 213 // the very first enumerator's encoding if any. 214 auto first_child = enum_type->findOneChild<PDBSymbolData>(); 215 if (first_child) { 216 encoding = TranslateEnumEncoding(first_child->getValue().Type); 217 } 218 std::string name = enum_type->getName(); 219 uint64_t bytes = enum_type->getLength(); 220 CompilerType builtin_type; 221 if (bytes > 0) 222 builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize( 223 &m_ast, underlying_type_up.get(), encoding, bytes * 8); 224 else 225 builtin_type = m_ast.GetBasicType(eBasicTypeInt); 226 // FIXME: PDB does not have information about scoped enumeration (Enum Class). 227 // Set it false for now. 228 bool isScoped = false; 229 230 CompilerType ast_enum = m_ast.CreateEnumerationType( 231 name.c_str(), tu_decl_ctx, decl, builtin_type, isScoped); 232 auto enum_values = enum_type->findAllChildren<PDBSymbolData>(); 233 if (enum_values) { 234 while (auto enum_value = enum_values->getNext()) { 235 if (enum_value->getDataKind() != PDB_DataKind::Constant) 236 continue; 237 AddEnumValue(ast_enum, *enum_value); 238 } 239 } 240 if (ClangASTContext::StartTagDeclarationDefinition(ast_enum)) 241 ClangASTContext::CompleteTagDeclarationDefinition(ast_enum); 242 243 return std::make_shared<lldb_private::Type>( 244 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes, 245 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 246 ast_enum, lldb_private::Type::eResolveStateFull); 247 } else if (auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type)) { 248 lldb_private::Type *target_type = 249 m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId()); 250 if (!target_type) 251 return nullptr; 252 std::string name = type_def->getName(); 253 uint64_t bytes = type_def->getLength(); 254 CompilerType target_ast_type = target_type->GetFullCompilerType(); 255 CompilerDeclContext target_decl_ctx = 256 m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID()); 257 CompilerType ast_typedef = 258 m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx); 259 return std::make_shared<lldb_private::Type>( 260 type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 261 bytes, nullptr, target_type->GetID(), 262 lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef, 263 lldb_private::Type::eResolveStateFull); 264 } else if (auto func_sig = llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) { 265 auto arg_enum = func_sig->getArguments(); 266 uint32_t num_args = arg_enum->getChildCount(); 267 std::vector<CompilerType> arg_list; 268 269 bool is_variadic = func_sig->isCVarArgs(); 270 // Drop last variadic argument. 271 if (is_variadic) 272 --num_args; 273 for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) { 274 auto arg = arg_enum->getChildAtIndex(arg_idx); 275 if (!arg) 276 break; 277 lldb_private::Type *arg_type = 278 m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId()); 279 // If there's some error looking up one of the dependent types of this 280 // function signature, bail. 281 if (!arg_type) 282 return nullptr; 283 CompilerType arg_ast_type = arg_type->GetFullCompilerType(); 284 arg_list.push_back(arg_ast_type); 285 } 286 lldbassert(arg_list.size() <= num_args); 287 288 auto pdb_return_type = func_sig->getReturnType(); 289 lldb_private::Type *return_type = 290 m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId()); 291 // If there's some error looking up one of the dependent types of this 292 // function signature, bail. 293 if (!return_type) 294 return nullptr; 295 CompilerType return_ast_type = return_type->GetFullCompilerType(); 296 uint32_t type_quals = 0; 297 if (func_sig->isConstType()) 298 type_quals |= clang::Qualifiers::Const; 299 if (func_sig->isVolatileType()) 300 type_quals |= clang::Qualifiers::Volatile; 301 CompilerType func_sig_ast_type = m_ast.CreateFunctionType( 302 return_ast_type, arg_list.data(), arg_list.size(), is_variadic, 303 type_quals); 304 305 return std::make_shared<lldb_private::Type>( 306 func_sig->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 0, 307 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 308 func_sig_ast_type, lldb_private::Type::eResolveStateFull); 309 } else if (auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type)) { 310 uint32_t num_elements = array_type->getCount(); 311 uint32_t element_uid = array_type->getElementType()->getSymIndexId(); 312 uint32_t bytes = array_type->getLength(); 313 314 lldb_private::Type *element_type = 315 m_ast.GetSymbolFile()->ResolveTypeUID(element_uid); 316 if (!element_type) 317 return nullptr; 318 CompilerType element_ast_type = element_type->GetFullCompilerType(); 319 CompilerType array_ast_type = 320 m_ast.CreateArrayType(element_ast_type, num_elements, false); 321 return std::make_shared<lldb_private::Type>( 322 array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 323 bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, 324 decl, array_ast_type, lldb_private::Type::eResolveStateFull); 325 } else if (auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type)) { 326 PDB_BuiltinType builtin_kind = builtin_type->getBuiltinType(); 327 if (builtin_kind == PDB_BuiltinType::None) 328 return nullptr; 329 330 uint64_t bytes = builtin_type->getLength(); 331 Encoding encoding = TranslateBuiltinEncoding(builtin_kind); 332 CompilerType builtin_ast_type = GetBuiltinTypeForPDBEncodingAndBitSize( 333 &m_ast, builtin_type, encoding, bytes * 8); 334 335 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID; 336 if (builtin_type->isConstType()) { 337 encoding_data_type = Type::eEncodingIsConstUID; 338 builtin_ast_type = builtin_ast_type.AddConstModifier(); 339 } 340 if (builtin_type->isVolatileType()) { 341 encoding_data_type = Type::eEncodingIsVolatileUID; 342 builtin_ast_type = builtin_ast_type.AddVolatileModifier(); 343 } 344 auto type_name = GetPDBBuiltinTypeName(builtin_type, builtin_ast_type); 345 346 return std::make_shared<lldb_private::Type>( 347 builtin_type->getSymIndexId(), m_ast.GetSymbolFile(), type_name, 348 bytes, nullptr, LLDB_INVALID_UID, encoding_data_type, 349 decl, builtin_ast_type, lldb_private::Type::eResolveStateFull); 350 } else if (auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type)) { 351 Type *pointee_type = m_ast.GetSymbolFile()->ResolveTypeUID( 352 pointer_type->getPointeeType()->getSymIndexId()); 353 if (!pointee_type) 354 return nullptr; 355 356 CompilerType pointer_ast_type; 357 Type::EncodingDataType encoding_data_type = Type::eEncodingIsPointerUID; 358 if (pointer_type->isReference()) { 359 encoding_data_type = Type::eEncodingIsLValueReferenceUID; 360 pointer_ast_type = 361 pointee_type->GetFullCompilerType().GetLValueReferenceType(); 362 } else 363 pointer_ast_type = pointee_type->GetFullCompilerType().GetPointerType(); 364 365 return std::make_shared<lldb_private::Type>( 366 pointer_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 367 pointer_type->getLength(), nullptr, LLDB_INVALID_UID, 368 encoding_data_type, decl, pointer_ast_type, 369 lldb_private::Type::eResolveStateFull); 370 } 371 return nullptr; 372 } 373 374 bool PDBASTParser::AddEnumValue(CompilerType enum_type, 375 const PDBSymbolData &enum_value) const { 376 Declaration decl; 377 Variant v = enum_value.getValue(); 378 std::string name = enum_value.getName(); 379 int64_t raw_value; 380 switch (v.Type) { 381 case PDB_VariantType::Int8: 382 raw_value = v.Value.Int8; 383 break; 384 case PDB_VariantType::Int16: 385 raw_value = v.Value.Int16; 386 break; 387 case PDB_VariantType::Int32: 388 raw_value = v.Value.Int32; 389 break; 390 case PDB_VariantType::Int64: 391 raw_value = v.Value.Int64; 392 break; 393 case PDB_VariantType::UInt8: 394 raw_value = v.Value.UInt8; 395 break; 396 case PDB_VariantType::UInt16: 397 raw_value = v.Value.UInt16; 398 break; 399 case PDB_VariantType::UInt32: 400 raw_value = v.Value.UInt32; 401 break; 402 case PDB_VariantType::UInt64: 403 raw_value = v.Value.UInt64; 404 break; 405 default: 406 return false; 407 } 408 CompilerType underlying_type = 409 m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType()); 410 uint32_t byte_size = m_ast.getASTContext()->getTypeSize( 411 ClangUtil::GetQualType(underlying_type)); 412 return m_ast.AddEnumerationValueToEnumerationType( 413 enum_type.GetOpaqueQualType(), underlying_type, decl, name.c_str(), 414 raw_value, byte_size * 8); 415 } 416