107 const PDBSymbolTypeBuiltin &pdb_type,
108 Encoding encoding, uint32_t width) {
111 switch (pdb_type.getBuiltinType()) {
114 case PDB_BuiltinType::None:
116 case PDB_BuiltinType::Void:
118 case PDB_BuiltinType::Char:
120 case PDB_BuiltinType::Bool:
122 case PDB_BuiltinType::Long:
123 if (width == ast.getTypeSize(ast.LongTy))
125 ast.LongTy.getAsOpaquePtr());
126 if (width == ast.getTypeSize(ast.LongLongTy))
128 ast.LongLongTy.getAsOpaquePtr());
130 case PDB_BuiltinType::ULong:
131 if (width == ast.getTypeSize(ast.UnsignedLongTy))
133 ast.UnsignedLongTy.getAsOpaquePtr());
134 if (width == ast.getTypeSize(ast.UnsignedLongLongTy))
136 ast.UnsignedLongLongTy.getAsOpaquePtr());
138 case PDB_BuiltinType::WCharT:
139 if (width == ast.getTypeSize(ast.WCharTy))
141 ast.WCharTy.getAsOpaquePtr());
143 case PDB_BuiltinType::Char16:
145 ast.Char16Ty.getAsOpaquePtr());
146 case PDB_BuiltinType::Char32:
148 ast.Char32Ty.getAsOpaquePtr());
149 case PDB_BuiltinType::Float:
345 switch (type.getSymTag()) {
346 case PDB_SymType::BaseClass: {
347 auto symbol_file =
m_ast.GetSymbolFile();
351 auto ty = symbol_file->ResolveTypeUID(type.getRawSymbol().getTypeId());
352 return ty ? ty->shared_from_this() :
nullptr;
354 case PDB_SymType::UDT: {
355 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type);
366 if (udt->getLength() == 0)
383 m_ast.getASTContext(), name, decl_context);
388 metadata.
SetUserID(type.getSymIndexId());
391 clang_type =
m_ast.CreateRecordType(
401 auto inheritance_attr = clang::MSInheritanceAttr::CreateImplicit(
403 record_decl->addAttr(inheritance_attr);
407 auto children = udt->findAllChildren();
408 if (!children || children->getChildCount() == 0) {
430 if (udt->isConstType())
433 if (udt->isVolatileType())
437 return m_ast.GetSymbolFile()->MakeType(
438 type.getSymIndexId(),
ConstString(name), udt->getLength(),
nullptr,
442 case PDB_SymType::Enum: {
443 auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type);
449 uint64_t bytes = enum_type->getLength();
453 m_ast.getASTContext(), name, decl_context);
455 auto underlying_type_up = enum_type->getUnderlyingType();
456 if (!underlying_type_up)
463 auto first_child = enum_type->findOneChild<PDBSymbolData>();
470 m_ast, *underlying_type_up, encoding, bytes * 8);
476 bool isScoped =
false;
478 ast_enum =
m_ast.CreateEnumerationType(name, decl_context,
480 builtin_type, isScoped);
486 auto enum_values = enum_type->findAllChildren<PDBSymbolData>();
488 while (
auto enum_value = enum_values->getNext()) {
489 if (enum_value->getDataKind() != PDB_DataKind::Constant)
499 if (enum_type->isConstType())
502 if (enum_type->isVolatileType())
506 return m_ast.GetSymbolFile()->MakeType(
507 type.getSymIndexId(),
ConstString(name), bytes,
nullptr,
511 case PDB_SymType::Typedef: {
512 auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type);
530 m_ast.GetTypeForIdentifier<clang::TypedefNameDecl>(
531 m_ast.getASTContext(), name, decl_ctx);
536 name.c_str(),
m_ast.CreateDeclContext(decl_ctx), 0);
541 assert(typedef_decl);
545 if (type_def->isConstType())
548 if (type_def->isVolatileType())
552 std::optional<uint64_t> size;
553 if (type_def->getLength())
554 size = type_def->getLength();
555 return m_ast.GetSymbolFile()->MakeType(
556 type_def->getSymIndexId(),
ConstString(name), size,
nullptr,
560 case PDB_SymType::Function:
561 case PDB_SymType::FunctionSig: {
563 PDBSymbolTypeFunctionSig *func_sig =
nullptr;
564 if (
auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(&type)) {
565 if (pdb_func->isCompilerGenerated())
568 auto sig = pdb_func->getSignature();
571 func_sig = sig.release();
575 }
else if (
auto pdb_func_sig =
576 llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) {
577 func_sig =
const_cast<PDBSymbolTypeFunctionSig *
>(pdb_func_sig);
579 llvm_unreachable(
"Unexpected PDB symbol!");
581 auto arg_enum = func_sig->getArguments();
582 uint32_t num_args = arg_enum->getChildCount();
583 std::vector<CompilerType> arg_list;
585 bool is_variadic = func_sig->isCVarArgs();
589 for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) {
590 auto arg = arg_enum->getChildAtIndex(arg_idx);
605 arg_list.push_back(arg_ast_type);
609 auto pdb_return_type = func_sig->getReturnType();
621 uint32_t type_quals = 0;
622 if (func_sig->isConstType())
623 type_quals |= clang::Qualifiers::Const;
624 if (func_sig->isVolatileType())
625 type_quals |= clang::Qualifiers::Volatile;
628 return_ast_type, arg_list, is_variadic, type_quals, cc);
631 return m_ast.GetSymbolFile()->MakeType(
632 type.getSymIndexId(),
ConstString(name), std::nullopt,
nullptr,
636 case PDB_SymType::ArrayType: {
637 auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type);
639 uint32_t num_elements = array_type->getCount();
640 uint32_t element_uid = array_type->getElementTypeId();
641 std::optional<uint64_t> bytes;
642 if (uint64_t size = array_type->getLength())
667 element_ast_type, num_elements,
false);
669 array_type->getSymIndexId(),
ConstString(), bytes,
nullptr,
672 type_sp->SetEncodingType(element_type);
675 case PDB_SymType::BuiltinType: {
676 auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type);
677 assert(builtin_type);
678 PDB_BuiltinType builtin_kind = builtin_type->getBuiltinType();
679 if (builtin_kind == PDB_BuiltinType::None)
682 std::optional<uint64_t> bytes;
683 if (uint64_t size = builtin_type->getLength())
687 m_ast, *builtin_type, encoding, bytes.value_or(0) * 8);
689 if (builtin_type->isConstType())
692 if (builtin_type->isVolatileType())
697 return m_ast.GetSymbolFile()->MakeType(
698 builtin_type->getSymIndexId(), type_name, bytes,
nullptr,
702 case PDB_SymType::PointerType: {
703 auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type);
704 assert(pointer_type);
711 pointer_type->getPointeeType()->getSymIndexId());
715 if (pointer_type->isPointerToDataMember() ||
716 pointer_type->isPointerToMemberFunction()) {
717 auto class_parent_uid = pointer_type->getRawSymbol().getClassParentId();
718 auto class_parent_type = symbol_file->
ResolveTypeUID(class_parent_uid);
719 assert(class_parent_type);
723 class_parent_type->GetLayoutCompilerType(),
725 assert(pointer_ast_type);
727 return m_ast.GetSymbolFile()->MakeType(
736 if (pointer_type->isReference())
738 else if (pointer_type->isRValueReference())
743 if (pointer_type->isConstType())
746 if (pointer_type->isVolatileType())
749 if (pointer_type->isRestrictedType())
752 return m_ast.GetSymbolFile()->MakeType(
753 pointer_type->getSymIndexId(),
ConstString(), pointer_type->getLength(),
806 uint32_t sym_id = symbol.getSymIndexId();
812 m_ast.GetSymbolFile()->GetBackingSymbolFile());
818 auto tag = symbol.getSymTag();
819 if (tag == PDB_SymType::Data || tag == PDB_SymType::Function) {
820 const IPDBSession &session = symbol.getSession();
821 const IPDBRawSymbol &raw = symbol.getRawSymbol();
823 auto class_parent_id = raw.getClassParentId();
824 if (std::unique_ptr<PDBSymbol> class_parent =
825 session.getSymbolById(class_parent_id)) {
826 auto class_parent_type = symbol_file->ResolveTypeUID(class_parent_id);
827 if (!class_parent_type)
830 CompilerType class_parent_ct = class_parent_type->GetFullCompilerType();
840 if (
auto func = llvm::dyn_cast_or_null<PDBSymbolFunc>(&symbol)) {
843 if (uint32_t rva = func->getRelativeVirtualAddress()) {
846 class_parent->findAllChildren<PDBSymbolFunc>()) {
847 while (std::unique_ptr<PDBSymbolFunc> method =
848 methods_enum->getNext()) {
849 if (method->getRelativeVirtualAddress() == rva) {
873 switch (symbol.getSymTag()) {
874 case PDB_SymType::Data: {
875 auto data = llvm::dyn_cast<PDBSymbolData>(&symbol);
879 assert(decl_context);
886 if (
auto parent_decl = llvm::dyn_cast_or_null<clang::TagDecl>(decl_context))
887 m_ast.GetCompleteDecl(parent_decl);
896 auto type = symbol_file->ResolveTypeUID(data->getTypeId());
900 decl =
m_ast.CreateVariableDeclaration(
909 case PDB_SymType::Function: {
910 auto func = llvm::dyn_cast<PDBSymbolFunc>(&symbol);
914 assert(decl_context);
919 Type *type = symbol_file->ResolveTypeUID(sym_id);
923 auto storage = func->isStatic() ? clang::StorageClass::SC_Static
924 : clang::StorageClass::SC_None;
926 auto decl =
m_ast.CreateFunctionDeclaration(
931 std::vector<clang::ParmVarDecl *> params;
932 if (std::unique_ptr<PDBSymbolTypeFunctionSig> sig = func->getSignature()) {
934 arg_enum = sig->findAllChildren<PDBSymbolTypeFunctionArg>()) {
935 while (std::unique_ptr<PDBSymbolTypeFunctionArg> arg =
936 arg_enum->getNext()) {
937 Type *arg_type = symbol_file->ResolveTypeUID(arg->getTypeId());
941 clang::ParmVarDecl *param =
m_ast.CreateParameterDeclaration(
945 params.push_back(param);
949 if (params.size() && decl)
950 decl->setParams(params);
958 Type *type = symbol_file->ResolveTypeUID(sym_id);
998 const llvm::pdb::PDBSymbol &symbol) {
1002 return parent_context;
1014 std::string name(symbol.getRawSymbol().getName());
1016 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.
GetSpecifiers();
1018 return m_ast.GetTranslationUnitDecl();
1021 m_ast.GetSymbolFile()->GetBackingSymbolFile());
1023 return m_ast.GetTranslationUnitDecl();
1025 auto global = symbol_file->GetPDBSession().getGlobalScope();
1027 return m_ast.GetTranslationUnitDecl();
1029 bool has_type_or_function_parent =
false;
1030 clang::DeclContext *curr_context =
m_ast.GetTranslationUnitDecl();
1031 for (std::size_t i = 0; i < specs.size() - 1; i++) {
1033 if (std::unique_ptr<IPDBEnumSymbols> children_enum = global->findChildren(
1034 PDB_SymType::None, specs[i].GetFullName(), NS_CaseSensitive)) {
1035 while (IPDBEnumChildren<PDBSymbol>::ChildTypePtr child =
1036 children_enum->getNext()) {
1037 if (clang::DeclContext *child_context =
1043 has_type_or_function_parent =
true;
1044 curr_context = child_context;
1053 if (!has_type_or_function_parent) {
1054 std::string namespace_name = std::string(specs[i].GetBaseName());
1055 const char *namespace_name_c_str =
1057 : namespace_name.data();
1058 clang::NamespaceDecl *namespace_decl =
1059 m_ast.GetUniqueNamespaceDeclaration(
1065 curr_context = namespace_decl;
1069 return curr_context;
1216 while (
auto member = members_enum.getNext()) {
1217 if (member->isCompilerGenerated())
1220 auto member_name = member->getName();
1222 auto member_type = symbol_file.
ResolveTypeUID(member->getTypeId());
1227 if (!member_comp_type.GetCompleteType()) {
1229 ":: Class '{0}' has a member '{1}' of type '{2}' "
1230 "which does not have a complete definition.",
1232 member_comp_type.GetTypeName().GetCString());
1237 switch (member->getDataKind()) {
1238 case PDB_DataKind::Member: {
1239 auto location_type = member->getLocationType();
1241 auto bit_size = member->getLength();
1242 if (location_type == PDB_LocType::ThisRel)
1246 record_type, member_name.c_str(), member_comp_type, bit_size);
1252 auto offset = member->getOffset() * 8;
1253 if (location_type == PDB_LocType::BitField)
1254 offset += member->getBitPosition();
1256 layout_info.
field_offsets.insert(std::make_pair(decl, offset));
1260 case PDB_DataKind::StaticMember: {
1262 record_type, member_name.c_str(), member_comp_type);
1268 if (member_comp_type.IsConst()) {
1269 auto value = member->getValue();
1270 if (value.Type == llvm::pdb::Empty) {
1272 "Class '{0}' has member '{1}' of type '{2}' with an unknown "
1275 member_comp_type.GetTypeName());
1279 clang::QualType qual_type = decl->getType();
1280 unsigned type_width =
m_ast.getASTContext().getIntWidth(qual_type);
1281 unsigned constant_width = value.getBitWidth();
1283 if (qual_type->isIntegralOrEnumerationType()) {
1284 if (type_width >= constant_width) {
1286 decl, value.toAPSInt().extOrTrunc(type_width));
1289 "Class '{0}' has a member '{1}' of type '{2}' ({3} bits) "
1290 "which resolves to a wider constant value ({4} bits). "
1291 "Ignoring constant.",
1293 member_comp_type.GetTypeName(), type_width,
1297 switch (member_comp_type.GetBasicTypeEnumeration()) {
1301 if (type_width == constant_width) {
1303 decl, value.toAPFloat());
1304 decl->setConstexpr(
true);
1307 "Class '{0}' has a member '{1}' of type '{2}' ({3} "
1308 "bits) which resolves to a constant value of mismatched "
1309 "width ({4} bits). Ignoring constant.",
1311 member_comp_type.GetTypeName(), type_width,
1326 llvm_unreachable(
"unsupported PDB data kind");