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:
373 switch (type.getSymTag()) {
374 case PDB_SymType::BaseClass: {
375 auto symbol_file =
m_ast.GetSymbolFile();
379 auto ty = symbol_file->ResolveTypeUID(type.getRawSymbol().getTypeId());
380 return ty ? ty->shared_from_this() :
nullptr;
382 case PDB_SymType::UDT: {
383 auto udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&type);
394 if (udt->getLength() == 0)
411 m_ast.getASTContext(), name, decl_context);
418 metadata.
SetUserID(type.getSymIndexId());
421 clang_type =
m_ast.CreateRecordType(
431 auto inheritance_attr = clang::MSInheritanceAttr::CreateImplicit(
433 record_decl->addAttr(inheritance_attr);
437 auto children = udt->findAllChildren();
438 if (!children || children->getChildCount() == 0) {
460 if (udt->isConstType())
463 if (udt->isVolatileType())
467 return m_ast.GetSymbolFile()->MakeType(
468 type.getSymIndexId(),
ConstString(name), udt->getLength(),
nullptr,
472 case PDB_SymType::Enum: {
473 auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(&type);
479 uint64_t bytes = enum_type->getLength();
483 m_ast.getASTContext(), name, decl_context);
485 auto underlying_type_up = enum_type->getUnderlyingType();
486 if (!underlying_type_up)
493 auto first_child = enum_type->findOneChild<PDBSymbolData>();
500 m_ast, *underlying_type_up, encoding, bytes * 8);
506 bool isScoped =
false;
508 ast_enum =
m_ast.CreateEnumerationType(name, decl_context,
510 builtin_type, isScoped);
516 auto enum_values = enum_type->findAllChildren<PDBSymbolData>();
518 while (
auto enum_value = enum_values->getNext()) {
519 if (enum_value->getDataKind() != PDB_DataKind::Constant)
529 if (enum_type->isConstType())
532 if (enum_type->isVolatileType())
536 return m_ast.GetSymbolFile()->MakeType(
537 type.getSymIndexId(),
ConstString(name), bytes,
nullptr,
541 case PDB_SymType::Typedef: {
542 auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type);
560 m_ast.GetTypeForIdentifier<clang::TypedefNameDecl>(
561 m_ast.getASTContext(), name, decl_ctx);
566 name.c_str(),
m_ast.CreateDeclContext(decl_ctx), 0);
571 assert(typedef_decl);
575 if (type_def->isConstType())
578 if (type_def->isVolatileType())
582 std::optional<uint64_t> size;
583 if (type_def->getLength())
584 size = type_def->getLength();
585 return m_ast.GetSymbolFile()->MakeType(
586 type_def->getSymIndexId(),
ConstString(name), size,
nullptr,
590 case PDB_SymType::Function:
591 case PDB_SymType::FunctionSig: {
593 PDBSymbolTypeFunctionSig *func_sig =
nullptr;
594 if (
auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(&type)) {
595 if (pdb_func->isCompilerGenerated())
598 auto sig = pdb_func->getSignature();
601 func_sig = sig.release();
605 }
else if (
auto pdb_func_sig =
606 llvm::dyn_cast<PDBSymbolTypeFunctionSig>(&type)) {
607 func_sig =
const_cast<PDBSymbolTypeFunctionSig *
>(pdb_func_sig);
609 llvm_unreachable(
"Unexpected PDB symbol!");
611 auto arg_enum = func_sig->getArguments();
612 uint32_t num_args = arg_enum->getChildCount();
613 std::vector<CompilerType> arg_list;
615 bool is_variadic = func_sig->isCVarArgs();
619 for (uint32_t arg_idx = 0; arg_idx < num_args; arg_idx++) {
620 auto arg = arg_enum->getChildAtIndex(arg_idx);
635 arg_list.push_back(arg_ast_type);
639 auto pdb_return_type = func_sig->getReturnType();
651 uint32_t type_quals = 0;
652 if (func_sig->isConstType())
653 type_quals |= clang::Qualifiers::Const;
654 if (func_sig->isVolatileType())
655 type_quals |= clang::Qualifiers::Volatile;
658 return_ast_type, arg_list, is_variadic, type_quals, cc);
661 return m_ast.GetSymbolFile()->MakeType(
662 type.getSymIndexId(),
ConstString(name), std::nullopt,
nullptr,
666 case PDB_SymType::ArrayType: {
667 auto array_type = llvm::dyn_cast<PDBSymbolTypeArray>(&type);
669 uint32_t num_elements = array_type->getCount();
670 uint32_t element_uid = array_type->getElementTypeId();
671 std::optional<uint64_t> bytes;
672 if (uint64_t size = array_type->getLength())
697 element_ast_type, num_elements,
false);
699 array_type->getSymIndexId(),
ConstString(), bytes,
nullptr,
702 type_sp->SetEncodingType(element_type);
705 case PDB_SymType::BuiltinType: {
706 auto *builtin_type = llvm::dyn_cast<PDBSymbolTypeBuiltin>(&type);
707 assert(builtin_type);
708 PDB_BuiltinType builtin_kind = builtin_type->getBuiltinType();
709 if (builtin_kind == PDB_BuiltinType::None)
712 std::optional<uint64_t> bytes;
713 if (uint64_t size = builtin_type->getLength())
717 m_ast, *builtin_type, encoding, bytes.value_or(0) * 8);
719 if (builtin_type->isConstType())
722 if (builtin_type->isVolatileType())
727 return m_ast.GetSymbolFile()->MakeType(
728 builtin_type->getSymIndexId(), type_name, bytes,
nullptr,
732 case PDB_SymType::PointerType: {
733 auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type);
734 assert(pointer_type);
741 pointer_type->getPointeeType()->getSymIndexId());
745 if (pointer_type->isPointerToDataMember() ||
746 pointer_type->isPointerToMemberFunction()) {
747 auto class_parent_uid = pointer_type->getRawSymbol().getClassParentId();
748 auto class_parent_type = symbol_file->
ResolveTypeUID(class_parent_uid);
749 assert(class_parent_type);
753 class_parent_type->GetLayoutCompilerType(),
755 assert(pointer_ast_type);
757 return m_ast.GetSymbolFile()->MakeType(
766 if (pointer_type->isReference())
768 else if (pointer_type->isRValueReference())
773 if (pointer_type->isConstType())
776 if (pointer_type->isVolatileType())
779 if (pointer_type->isRestrictedType())
782 return m_ast.GetSymbolFile()->MakeType(
783 pointer_type->getSymIndexId(),
ConstString(), pointer_type->getLength(),
836 uint32_t sym_id = symbol.getSymIndexId();
842 m_ast.GetSymbolFile()->GetBackingSymbolFile());
848 auto tag = symbol.getSymTag();
849 if (tag == PDB_SymType::Data || tag == PDB_SymType::Function) {
850 const IPDBSession &session = symbol.getSession();
851 const IPDBRawSymbol &raw = symbol.getRawSymbol();
853 auto class_parent_id = raw.getClassParentId();
854 if (std::unique_ptr<PDBSymbol> class_parent =
855 session.getSymbolById(class_parent_id)) {
856 auto class_parent_type = symbol_file->ResolveTypeUID(class_parent_id);
857 if (!class_parent_type)
860 CompilerType class_parent_ct = class_parent_type->GetFullCompilerType();
870 if (
auto func = llvm::dyn_cast_or_null<PDBSymbolFunc>(&symbol)) {
873 if (uint32_t rva = func->getRelativeVirtualAddress()) {
876 class_parent->findAllChildren<PDBSymbolFunc>()) {
877 while (std::unique_ptr<PDBSymbolFunc> method =
878 methods_enum->getNext()) {
879 if (method->getRelativeVirtualAddress() == rva) {
903 switch (symbol.getSymTag()) {
904 case PDB_SymType::Data: {
905 auto data = llvm::dyn_cast<PDBSymbolData>(&symbol);
909 assert(decl_context);
916 if (
auto parent_decl = llvm::dyn_cast_or_null<clang::TagDecl>(decl_context))
917 m_ast.GetCompleteDecl(parent_decl);
926 auto type = symbol_file->ResolveTypeUID(data->getTypeId());
930 decl =
m_ast.CreateVariableDeclaration(
939 case PDB_SymType::Function: {
940 auto func = llvm::dyn_cast<PDBSymbolFunc>(&symbol);
944 assert(decl_context);
949 Type *type = symbol_file->ResolveTypeUID(sym_id);
953 auto storage = func->isStatic() ? clang::StorageClass::SC_Static
954 : clang::StorageClass::SC_None;
956 auto decl =
m_ast.CreateFunctionDeclaration(
961 std::vector<clang::ParmVarDecl *> params;
962 if (std::unique_ptr<PDBSymbolTypeFunctionSig> sig = func->getSignature()) {
964 arg_enum = sig->findAllChildren<PDBSymbolTypeFunctionArg>()) {
965 while (std::unique_ptr<PDBSymbolTypeFunctionArg> arg =
966 arg_enum->getNext()) {
967 Type *arg_type = symbol_file->ResolveTypeUID(arg->getTypeId());
971 clang::ParmVarDecl *param =
m_ast.CreateParameterDeclaration(
975 params.push_back(param);
979 if (params.size() && decl)
980 decl->setParams(params);
988 Type *type = symbol_file->ResolveTypeUID(sym_id);
1028 const llvm::pdb::PDBSymbol &symbol) {
1032 return parent_context;
1044 std::string name(symbol.getRawSymbol().getName());
1046 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.
GetSpecifiers();
1048 return m_ast.GetTranslationUnitDecl();
1051 m_ast.GetSymbolFile()->GetBackingSymbolFile());
1053 return m_ast.GetTranslationUnitDecl();
1055 auto global = symbol_file->GetPDBSession().getGlobalScope();
1057 return m_ast.GetTranslationUnitDecl();
1059 bool has_type_or_function_parent =
false;
1060 clang::DeclContext *curr_context =
m_ast.GetTranslationUnitDecl();
1061 for (std::size_t i = 0; i < specs.size() - 1; i++) {
1063 if (std::unique_ptr<IPDBEnumSymbols> children_enum = global->findChildren(
1064 PDB_SymType::None, specs[i].GetFullName(), NS_CaseSensitive)) {
1065 while (IPDBEnumChildren<PDBSymbol>::ChildTypePtr child =
1066 children_enum->getNext()) {
1067 if (clang::DeclContext *child_context =
1073 has_type_or_function_parent =
true;
1074 curr_context = child_context;
1083 if (!has_type_or_function_parent) {
1084 std::string namespace_name = std::string(specs[i].GetBaseName());
1085 const char *namespace_name_c_str =
1087 : namespace_name.data();
1088 clang::NamespaceDecl *namespace_decl =
1089 m_ast.GetUniqueNamespaceDeclaration(
1095 curr_context = namespace_decl;
1099 return curr_context;
1246 while (
auto member = members_enum.getNext()) {
1247 if (member->isCompilerGenerated())
1250 auto member_name = member->getName();
1252 auto member_type = symbol_file.
ResolveTypeUID(member->getTypeId());
1257 if (!member_comp_type.GetCompleteType()) {
1259 ":: Class '{0}' has a member '{1}' of type '{2}' "
1260 "which does not have a complete definition.",
1262 member_comp_type.GetTypeName().GetCString());
1269 switch (member->getDataKind()) {
1270 case PDB_DataKind::Member: {
1271 auto location_type = member->getLocationType();
1273 auto bit_size = member->getLength();
1274 if (location_type == PDB_LocType::ThisRel)
1278 record_type, member_name.c_str(), member_comp_type, access, bit_size);
1284 auto offset = member->getOffset() * 8;
1285 if (location_type == PDB_LocType::BitField)
1286 offset += member->getBitPosition();
1288 layout_info.
field_offsets.insert(std::make_pair(decl, offset));
1292 case PDB_DataKind::StaticMember: {
1294 record_type, member_name.c_str(), member_comp_type, access);
1300 if (member_comp_type.IsConst()) {
1301 auto value = member->getValue();
1302 if (value.Type == llvm::pdb::Empty) {
1304 "Class '{0}' has member '{1}' of type '{2}' with an unknown "
1307 member_comp_type.GetTypeName());
1311 clang::QualType qual_type = decl->getType();
1312 unsigned type_width =
m_ast.getASTContext().getIntWidth(qual_type);
1313 unsigned constant_width = value.getBitWidth();
1315 if (qual_type->isIntegralOrEnumerationType()) {
1316 if (type_width >= constant_width) {
1318 decl, value.toAPSInt().extOrTrunc(type_width));
1321 "Class '{0}' has a member '{1}' of type '{2}' ({3} bits) "
1322 "which resolves to a wider constant value ({4} bits). "
1323 "Ignoring constant.",
1325 member_comp_type.GetTypeName(), type_width,
1329 switch (member_comp_type.GetBasicTypeEnumeration()) {
1333 if (type_width == constant_width) {
1335 decl, value.toAPFloat());
1336 decl->setConstexpr(
true);
1339 "Class '{0}' has a member '{1}' of type '{2}' ({3} "
1340 "bits) which resolves to a constant value of mismatched "
1341 "width ({4} bits). Ignoring constant.",
1343 member_comp_type.GetTypeName(), type_width,
1358 llvm_unreachable(
"unsupported PDB data kind");