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/PDBSymbolTypeTypedef.h" 30 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" 31 32 using namespace lldb; 33 using namespace lldb_private; 34 using namespace llvm; 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 clang::TTK_Class; 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 71 PDBASTParser::PDBASTParser(lldb_private::ClangASTContext &ast) : m_ast(ast) {} 72 73 PDBASTParser::~PDBASTParser() {} 74 75 // DebugInfoASTParser interface 76 77 lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) { 78 // PDB doesn't maintain enough information to robustly rebuild the entire 79 // tree, and this is most problematic when it comes to figure out the 80 // right DeclContext to put a type in. So for now, everything goes in 81 // the translation unit decl as a fully qualified type. 82 clang::DeclContext *tu_decl_ctx = m_ast.GetTranslationUnitDecl(); 83 Declaration decl; 84 85 if (auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type)) { 86 AccessType access = lldb::eAccessPublic; 87 PDB_UdtType udt_kind = udt->getUdtKind(); 88 89 if (udt_kind == PDB_UdtType::Class) 90 access = lldb::eAccessPrivate; 91 92 CompilerType clang_type = m_ast.CreateRecordType( 93 tu_decl_ctx, access, udt->getName().c_str(), TranslateUdtKind(udt_kind), 94 lldb::eLanguageTypeC_plus_plus, nullptr); 95 96 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true); 97 98 return std::make_shared<lldb_private::Type>( 99 type.getSymIndexId(), m_ast.GetSymbolFile(), 100 ConstString(udt->getName()), udt->getLength(), nullptr, 101 LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, clang_type, 102 lldb_private::Type::eResolveStateForward); 103 } else if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type)) { 104 std::string name = enum_type->getName(); 105 lldb::Encoding encoding = 106 TranslateBuiltinEncoding(enum_type->getBuiltinType()); 107 uint64_t bytes = enum_type->getLength(); 108 CompilerType builtin_type = 109 m_ast.GetBuiltinTypeForEncodingAndBitSize(encoding, bytes * 8); 110 111 CompilerType ast_enum = m_ast.CreateEnumerationType( 112 name.c_str(), tu_decl_ctx, decl, builtin_type, false); 113 auto enum_values = enum_type->findAllChildren<PDBSymbolData>(); 114 while (auto enum_value = enum_values->getNext()) { 115 if (enum_value->getDataKind() != PDB_DataKind::Constant) 116 continue; 117 AddEnumValue(ast_enum, *enum_value); 118 } 119 120 return std::make_shared<lldb_private::Type>( 121 type.getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), bytes, 122 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 123 ast_enum, lldb_private::Type::eResolveStateFull); 124 } else if (auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type)) { 125 lldb_private::Type *target_type = 126 m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId()); 127 if (!target_type) 128 return nullptr; 129 std::string name = type_def->getName(); 130 uint64_t bytes = type_def->getLength(); 131 CompilerType target_ast_type = target_type->GetFullCompilerType(); 132 CompilerDeclContext target_decl_ctx = 133 m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID()); 134 CompilerType ast_typedef = 135 m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx); 136 return std::make_shared<lldb_private::Type>( 137 type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 138 bytes, nullptr, target_type->GetID(), 139 lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef, 140 lldb_private::Type::eResolveStateFull); 141 } else if (auto func_sig = llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) { 142 auto arg_enum = func_sig->getArguments(); 143 uint32_t num_args = arg_enum->getChildCount(); 144 std::vector<CompilerType> arg_list(num_args); 145 while (auto arg = arg_enum->getNext()) { 146 lldb_private::Type *arg_type = 147 m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId()); 148 // If there's some error looking up one of the dependent types of this 149 // function signature, bail. 150 if (!arg_type) 151 return nullptr; 152 CompilerType arg_ast_type = arg_type->GetFullCompilerType(); 153 arg_list.push_back(arg_ast_type); 154 } 155 auto pdb_return_type = func_sig->getReturnType(); 156 lldb_private::Type *return_type = 157 m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId()); 158 // If there's some error looking up one of the dependent types of this 159 // function signature, bail. 160 if (!return_type) 161 return nullptr; 162 CompilerType return_ast_type = return_type->GetFullCompilerType(); 163 uint32_t type_quals = 0; 164 if (func_sig->isConstType()) 165 type_quals |= clang::Qualifiers::Const; 166 if (func_sig->isVolatileType()) 167 type_quals |= clang::Qualifiers::Volatile; 168 CompilerType func_sig_ast_type = m_ast.CreateFunctionType( 169 return_ast_type, &arg_list[0], num_args, false, type_quals); 170 171 return std::make_shared<lldb_private::Type>( 172 func_sig->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 0, 173 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 174 func_sig_ast_type, lldb_private::Type::eResolveStateFull); 175 } else if (auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type)) { 176 uint32_t num_elements = array_type->getCount(); 177 uint32_t element_uid = array_type->getElementType()->getSymIndexId(); 178 uint32_t bytes = array_type->getLength(); 179 180 lldb_private::Type *element_type = 181 m_ast.GetSymbolFile()->ResolveTypeUID(element_uid); 182 if (!element_type) 183 return nullptr; 184 CompilerType element_ast_type = element_type->GetFullCompilerType(); 185 CompilerType array_ast_type = 186 m_ast.CreateArrayType(element_ast_type, num_elements, false); 187 return std::make_shared<lldb_private::Type>( 188 array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 189 bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, 190 decl, array_ast_type, lldb_private::Type::eResolveStateFull); 191 } 192 return nullptr; 193 } 194 195 bool PDBASTParser::AddEnumValue(CompilerType enum_type, 196 const PDBSymbolData &enum_value) const { 197 Declaration decl; 198 Variant v = enum_value.getValue(); 199 std::string name = enum_value.getName(); 200 int64_t raw_value; 201 switch (v.Type) { 202 case PDB_VariantType::Int8: 203 raw_value = v.Value.Int8; 204 break; 205 case PDB_VariantType::Int16: 206 raw_value = v.Value.Int16; 207 break; 208 case PDB_VariantType::Int32: 209 raw_value = v.Value.Int32; 210 break; 211 case PDB_VariantType::Int64: 212 raw_value = v.Value.Int64; 213 break; 214 case PDB_VariantType::UInt8: 215 raw_value = v.Value.UInt8; 216 break; 217 case PDB_VariantType::UInt16: 218 raw_value = v.Value.UInt16; 219 break; 220 case PDB_VariantType::UInt32: 221 raw_value = v.Value.UInt32; 222 break; 223 case PDB_VariantType::UInt64: 224 raw_value = v.Value.UInt64; 225 break; 226 default: 227 return false; 228 } 229 CompilerType underlying_type = 230 m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType()); 231 uint32_t byte_size = m_ast.getASTContext()->getTypeSize( 232 ClangUtil::GetQualType(underlying_type)); 233 return m_ast.AddEnumerationValueToEnumerationType( 234 enum_type.GetOpaqueQualType(), underlying_type, decl, name.c_str(), 235 raw_value, byte_size * 8); 236 } 237