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 if (!target_type) 132 return nullptr; 133 CompilerType target_ast_type = target_type->GetFullCompilerType(); 134 CompilerDeclContext target_decl_ctx = 135 m_ast.GetSymbolFile()->GetDeclContextForUID(target_type->GetID()); 136 CompilerType ast_typedef = 137 m_ast.CreateTypedefType(target_ast_type, name.c_str(), target_decl_ctx); 138 return std::make_shared<lldb_private::Type>( 139 type_def->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(name), 140 bytes, nullptr, target_type->GetID(), 141 lldb_private::Type::eEncodingIsTypedefUID, decl, ast_typedef, 142 lldb_private::Type::eResolveStateFull); 143 } else if (auto func_sig = llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) { 144 auto arg_enum = func_sig->getArguments(); 145 uint32_t num_args = arg_enum->getChildCount(); 146 std::vector<CompilerType> arg_list(num_args); 147 while (auto arg = arg_enum->getNext()) { 148 lldb_private::Type *arg_type = 149 m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId()); 150 // If there's some error looking up one of the dependent types of this 151 // function signature, bail. 152 if (!arg_type) 153 return nullptr; 154 CompilerType arg_ast_type = arg_type->GetFullCompilerType(); 155 arg_list.push_back(arg_ast_type); 156 } 157 auto pdb_return_type = func_sig->getReturnType(); 158 lldb_private::Type *return_type = 159 m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId()); 160 // If there's some error looking up one of the dependent types of this 161 // function signature, bail. 162 if (!return_type) 163 return nullptr; 164 CompilerType return_ast_type = return_type->GetFullCompilerType(); 165 uint32_t type_quals = 0; 166 if (func_sig->isConstType()) 167 type_quals |= clang::Qualifiers::Const; 168 if (func_sig->isVolatileType()) 169 type_quals |= clang::Qualifiers::Volatile; 170 CompilerType func_sig_ast_type = m_ast.CreateFunctionType( 171 return_ast_type, &arg_list[0], num_args, false, type_quals); 172 173 return std::make_shared<lldb_private::Type>( 174 func_sig->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 0, 175 nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, 176 func_sig_ast_type, lldb_private::Type::eResolveStateFull); 177 } else if (auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type)) { 178 uint32_t num_elements = array_type->getCount(); 179 uint32_t element_uid = array_type->getElementType()->getSymIndexId(); 180 uint32_t bytes = array_type->getLength(); 181 182 lldb_private::Type *element_type = 183 m_ast.GetSymbolFile()->ResolveTypeUID(element_uid); 184 if (!element_type) 185 return nullptr; 186 CompilerType element_ast_type = element_type->GetFullCompilerType(); 187 CompilerType array_ast_type = 188 m_ast.CreateArrayType(element_ast_type, num_elements, false); 189 return std::make_shared<lldb_private::Type>( 190 array_type->getSymIndexId(), m_ast.GetSymbolFile(), ConstString(), 191 bytes, nullptr, LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, 192 decl, array_ast_type, lldb_private::Type::eResolveStateFull); 193 } 194 return nullptr; 195 } 196 197 bool PDBASTParser::AddEnumValue(CompilerType enum_type, 198 const PDBSymbolData &enum_value) const { 199 Declaration decl; 200 Variant v = enum_value.getValue(); 201 std::string name = enum_value.getName(); 202 int64_t raw_value; 203 switch (v.Type) { 204 case PDB_VariantType::Int8: 205 raw_value = v.Value.Int8; 206 break; 207 case PDB_VariantType::Int16: 208 raw_value = v.Value.Int16; 209 break; 210 case PDB_VariantType::Int32: 211 raw_value = v.Value.Int32; 212 break; 213 case PDB_VariantType::Int64: 214 raw_value = v.Value.Int64; 215 break; 216 case PDB_VariantType::UInt8: 217 raw_value = v.Value.UInt8; 218 break; 219 case PDB_VariantType::UInt16: 220 raw_value = v.Value.UInt16; 221 break; 222 case PDB_VariantType::UInt32: 223 raw_value = v.Value.UInt32; 224 break; 225 case PDB_VariantType::UInt64: 226 raw_value = v.Value.UInt64; 227 break; 228 default: 229 return false; 230 } 231 CompilerType underlying_type = 232 m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType()); 233 uint32_t byte_size = m_ast.getASTContext()->getTypeSize( 234 ClangUtil::GetQualType(underlying_type)); 235 return m_ast.AddEnumerationValueToEnumerationType( 236 enum_type.GetOpaqueQualType(), underlying_type, decl, name.c_str(), 237 raw_value, byte_size * 8); 238 } 239