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