Path: blob/main/contrib/llvm-project/clang/lib/Serialization/ASTCommon.cpp
35234 views
//===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines common functions that both ASTReader and ASTWriter use.9//10//===----------------------------------------------------------------------===//1112#include "ASTCommon.h"13#include "clang/AST/DeclCXX.h"14#include "clang/AST/DeclObjC.h"15#include "clang/Basic/IdentifierTable.h"16#include "clang/Serialization/ASTDeserializationListener.h"17#include "llvm/Support/DJB.h"1819using namespace clang;2021// Give ASTDeserializationListener's VTable a home.22ASTDeserializationListener::~ASTDeserializationListener() { }2324serialization::TypeIdx25serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {26unsigned ID = 0;27switch (BT->getKind()) {28case BuiltinType::Void:29ID = PREDEF_TYPE_VOID_ID;30break;31case BuiltinType::Bool:32ID = PREDEF_TYPE_BOOL_ID;33break;34case BuiltinType::Char_U:35ID = PREDEF_TYPE_CHAR_U_ID;36break;37case BuiltinType::UChar:38ID = PREDEF_TYPE_UCHAR_ID;39break;40case BuiltinType::UShort:41ID = PREDEF_TYPE_USHORT_ID;42break;43case BuiltinType::UInt:44ID = PREDEF_TYPE_UINT_ID;45break;46case BuiltinType::ULong:47ID = PREDEF_TYPE_ULONG_ID;48break;49case BuiltinType::ULongLong:50ID = PREDEF_TYPE_ULONGLONG_ID;51break;52case BuiltinType::UInt128:53ID = PREDEF_TYPE_UINT128_ID;54break;55case BuiltinType::Char_S:56ID = PREDEF_TYPE_CHAR_S_ID;57break;58case BuiltinType::SChar:59ID = PREDEF_TYPE_SCHAR_ID;60break;61case BuiltinType::WChar_S:62case BuiltinType::WChar_U:63ID = PREDEF_TYPE_WCHAR_ID;64break;65case BuiltinType::Short:66ID = PREDEF_TYPE_SHORT_ID;67break;68case BuiltinType::Int:69ID = PREDEF_TYPE_INT_ID;70break;71case BuiltinType::Long:72ID = PREDEF_TYPE_LONG_ID;73break;74case BuiltinType::LongLong:75ID = PREDEF_TYPE_LONGLONG_ID;76break;77case BuiltinType::Int128:78ID = PREDEF_TYPE_INT128_ID;79break;80case BuiltinType::Half:81ID = PREDEF_TYPE_HALF_ID;82break;83case BuiltinType::Float:84ID = PREDEF_TYPE_FLOAT_ID;85break;86case BuiltinType::Double:87ID = PREDEF_TYPE_DOUBLE_ID;88break;89case BuiltinType::LongDouble:90ID = PREDEF_TYPE_LONGDOUBLE_ID;91break;92case BuiltinType::ShortAccum:93ID = PREDEF_TYPE_SHORT_ACCUM_ID;94break;95case BuiltinType::Accum:96ID = PREDEF_TYPE_ACCUM_ID;97break;98case BuiltinType::LongAccum:99ID = PREDEF_TYPE_LONG_ACCUM_ID;100break;101case BuiltinType::UShortAccum:102ID = PREDEF_TYPE_USHORT_ACCUM_ID;103break;104case BuiltinType::UAccum:105ID = PREDEF_TYPE_UACCUM_ID;106break;107case BuiltinType::ULongAccum:108ID = PREDEF_TYPE_ULONG_ACCUM_ID;109break;110case BuiltinType::ShortFract:111ID = PREDEF_TYPE_SHORT_FRACT_ID;112break;113case BuiltinType::Fract:114ID = PREDEF_TYPE_FRACT_ID;115break;116case BuiltinType::LongFract:117ID = PREDEF_TYPE_LONG_FRACT_ID;118break;119case BuiltinType::UShortFract:120ID = PREDEF_TYPE_USHORT_FRACT_ID;121break;122case BuiltinType::UFract:123ID = PREDEF_TYPE_UFRACT_ID;124break;125case BuiltinType::ULongFract:126ID = PREDEF_TYPE_ULONG_FRACT_ID;127break;128case BuiltinType::SatShortAccum:129ID = PREDEF_TYPE_SAT_SHORT_ACCUM_ID;130break;131case BuiltinType::SatAccum:132ID = PREDEF_TYPE_SAT_ACCUM_ID;133break;134case BuiltinType::SatLongAccum:135ID = PREDEF_TYPE_SAT_LONG_ACCUM_ID;136break;137case BuiltinType::SatUShortAccum:138ID = PREDEF_TYPE_SAT_USHORT_ACCUM_ID;139break;140case BuiltinType::SatUAccum:141ID = PREDEF_TYPE_SAT_UACCUM_ID;142break;143case BuiltinType::SatULongAccum:144ID = PREDEF_TYPE_SAT_ULONG_ACCUM_ID;145break;146case BuiltinType::SatShortFract:147ID = PREDEF_TYPE_SAT_SHORT_FRACT_ID;148break;149case BuiltinType::SatFract:150ID = PREDEF_TYPE_SAT_FRACT_ID;151break;152case BuiltinType::SatLongFract:153ID = PREDEF_TYPE_SAT_LONG_FRACT_ID;154break;155case BuiltinType::SatUShortFract:156ID = PREDEF_TYPE_SAT_USHORT_FRACT_ID;157break;158case BuiltinType::SatUFract:159ID = PREDEF_TYPE_SAT_UFRACT_ID;160break;161case BuiltinType::SatULongFract:162ID = PREDEF_TYPE_SAT_ULONG_FRACT_ID;163break;164case BuiltinType::Float16:165ID = PREDEF_TYPE_FLOAT16_ID;166break;167case BuiltinType::Float128:168ID = PREDEF_TYPE_FLOAT128_ID;169break;170case BuiltinType::Ibm128:171ID = PREDEF_TYPE_IBM128_ID;172break;173case BuiltinType::NullPtr:174ID = PREDEF_TYPE_NULLPTR_ID;175break;176case BuiltinType::Char8:177ID = PREDEF_TYPE_CHAR8_ID;178break;179case BuiltinType::Char16:180ID = PREDEF_TYPE_CHAR16_ID;181break;182case BuiltinType::Char32:183ID = PREDEF_TYPE_CHAR32_ID;184break;185case BuiltinType::Overload:186ID = PREDEF_TYPE_OVERLOAD_ID;187break;188case BuiltinType::UnresolvedTemplate:189ID = PREDEF_TYPE_UNRESOLVED_TEMPLATE;190break;191case BuiltinType::BoundMember:192ID = PREDEF_TYPE_BOUND_MEMBER;193break;194case BuiltinType::PseudoObject:195ID = PREDEF_TYPE_PSEUDO_OBJECT;196break;197case BuiltinType::Dependent:198ID = PREDEF_TYPE_DEPENDENT_ID;199break;200case BuiltinType::UnknownAny:201ID = PREDEF_TYPE_UNKNOWN_ANY;202break;203case BuiltinType::ARCUnbridgedCast:204ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;205break;206case BuiltinType::ObjCId:207ID = PREDEF_TYPE_OBJC_ID;208break;209case BuiltinType::ObjCClass:210ID = PREDEF_TYPE_OBJC_CLASS;211break;212case BuiltinType::ObjCSel:213ID = PREDEF_TYPE_OBJC_SEL;214break;215#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \216case BuiltinType::Id: \217ID = PREDEF_TYPE_##Id##_ID; \218break;219#include "clang/Basic/OpenCLImageTypes.def"220#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \221case BuiltinType::Id: \222ID = PREDEF_TYPE_##Id##_ID; \223break;224#include "clang/Basic/OpenCLExtensionTypes.def"225case BuiltinType::OCLSampler:226ID = PREDEF_TYPE_SAMPLER_ID;227break;228case BuiltinType::OCLEvent:229ID = PREDEF_TYPE_EVENT_ID;230break;231case BuiltinType::OCLClkEvent:232ID = PREDEF_TYPE_CLK_EVENT_ID;233break;234case BuiltinType::OCLQueue:235ID = PREDEF_TYPE_QUEUE_ID;236break;237case BuiltinType::OCLReserveID:238ID = PREDEF_TYPE_RESERVE_ID_ID;239break;240#define SVE_TYPE(Name, Id, SingletonId) \241case BuiltinType::Id: \242ID = PREDEF_TYPE_##Id##_ID; \243break;244#include "clang/Basic/AArch64SVEACLETypes.def"245#define PPC_VECTOR_TYPE(Name, Id, Size) \246case BuiltinType::Id: \247ID = PREDEF_TYPE_##Id##_ID; \248break;249#include "clang/Basic/PPCTypes.def"250#define RVV_TYPE(Name, Id, SingletonId) \251case BuiltinType::Id: \252ID = PREDEF_TYPE_##Id##_ID; \253break;254#include "clang/Basic/RISCVVTypes.def"255#define WASM_TYPE(Name, Id, SingletonId) \256case BuiltinType::Id: \257ID = PREDEF_TYPE_##Id##_ID; \258break;259#include "clang/Basic/WebAssemblyReferenceTypes.def"260#define AMDGPU_TYPE(Name, Id, SingletonId) \261case BuiltinType::Id: \262ID = PREDEF_TYPE_##Id##_ID; \263break;264#include "clang/Basic/AMDGPUTypes.def"265case BuiltinType::BuiltinFn:266ID = PREDEF_TYPE_BUILTIN_FN;267break;268case BuiltinType::IncompleteMatrixIdx:269ID = PREDEF_TYPE_INCOMPLETE_MATRIX_IDX;270break;271case BuiltinType::ArraySection:272ID = PREDEF_TYPE_ARRAY_SECTION;273break;274case BuiltinType::OMPArrayShaping:275ID = PREDEF_TYPE_OMP_ARRAY_SHAPING;276break;277case BuiltinType::OMPIterator:278ID = PREDEF_TYPE_OMP_ITERATOR;279break;280case BuiltinType::BFloat16:281ID = PREDEF_TYPE_BFLOAT16_ID;282break;283}284285return TypeIdx(0, ID);286}287288unsigned serialization::ComputeHash(Selector Sel) {289unsigned N = Sel.getNumArgs();290if (N == 0)291++N;292unsigned R = 5381;293for (unsigned I = 0; I != N; ++I)294if (const IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))295R = llvm::djbHash(II->getName(), R);296return R;297}298299const DeclContext *300serialization::getDefinitiveDeclContext(const DeclContext *DC) {301switch (DC->getDeclKind()) {302// These entities may have multiple definitions.303case Decl::TranslationUnit:304case Decl::ExternCContext:305case Decl::Namespace:306case Decl::LinkageSpec:307case Decl::Export:308return nullptr;309310// C/C++ tag types can only be defined in one place.311case Decl::Enum:312case Decl::Record:313if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())314return Def;315return nullptr;316317// FIXME: These can be defined in one place... except special member318// functions and out-of-line definitions.319case Decl::CXXRecord:320case Decl::ClassTemplateSpecialization:321case Decl::ClassTemplatePartialSpecialization:322return nullptr;323324// Each function, method, and block declaration is its own DeclContext.325case Decl::Function:326case Decl::CXXMethod:327case Decl::CXXConstructor:328case Decl::CXXDestructor:329case Decl::CXXConversion:330case Decl::ObjCMethod:331case Decl::Block:332case Decl::Captured:333// Objective C categories, category implementations, and class334// implementations can only be defined in one place.335case Decl::ObjCCategory:336case Decl::ObjCCategoryImpl:337case Decl::ObjCImplementation:338return DC;339340case Decl::ObjCProtocol:341if (const ObjCProtocolDecl *Def342= cast<ObjCProtocolDecl>(DC)->getDefinition())343return Def;344return nullptr;345346// FIXME: These are defined in one place, but properties in class extensions347// end up being back-patched into the main interface. See348// SemaObjC::HandlePropertyInClassExtension for the offending code.349case Decl::ObjCInterface:350return nullptr;351352default:353llvm_unreachable("Unhandled DeclContext in AST reader");354}355356llvm_unreachable("Unhandled decl kind");357}358359bool serialization::isRedeclarableDeclKind(unsigned Kind) {360switch (static_cast<Decl::Kind>(Kind)) {361case Decl::TranslationUnit:362case Decl::ExternCContext:363// Special case of a "merged" declaration.364return true;365366case Decl::Namespace:367case Decl::NamespaceAlias:368case Decl::Typedef:369case Decl::TypeAlias:370case Decl::Enum:371case Decl::Record:372case Decl::CXXRecord:373case Decl::ClassTemplateSpecialization:374case Decl::ClassTemplatePartialSpecialization:375case Decl::VarTemplateSpecialization:376case Decl::VarTemplatePartialSpecialization:377case Decl::Function:378case Decl::CXXDeductionGuide:379case Decl::CXXMethod:380case Decl::CXXConstructor:381case Decl::CXXDestructor:382case Decl::CXXConversion:383case Decl::UsingShadow:384case Decl::ConstructorUsingShadow:385case Decl::Var:386case Decl::FunctionTemplate:387case Decl::ClassTemplate:388case Decl::VarTemplate:389case Decl::TypeAliasTemplate:390case Decl::ObjCProtocol:391case Decl::ObjCInterface:392case Decl::Empty:393return true;394395// Never redeclarable.396case Decl::UsingDirective:397case Decl::Label:398case Decl::UnresolvedUsingTypename:399case Decl::TemplateTypeParm:400case Decl::EnumConstant:401case Decl::UnresolvedUsingValue:402case Decl::IndirectField:403case Decl::Field:404case Decl::MSProperty:405case Decl::MSGuid:406case Decl::UnnamedGlobalConstant:407case Decl::TemplateParamObject:408case Decl::ObjCIvar:409case Decl::ObjCAtDefsField:410case Decl::NonTypeTemplateParm:411case Decl::TemplateTemplateParm:412case Decl::Using:413case Decl::UsingEnum:414case Decl::UsingPack:415case Decl::ObjCMethod:416case Decl::ObjCCategory:417case Decl::ObjCCategoryImpl:418case Decl::ObjCImplementation:419case Decl::ObjCProperty:420case Decl::ObjCCompatibleAlias:421case Decl::LinkageSpec:422case Decl::Export:423case Decl::ObjCPropertyImpl:424case Decl::PragmaComment:425case Decl::PragmaDetectMismatch:426case Decl::FileScopeAsm:427case Decl::TopLevelStmt:428case Decl::AccessSpec:429case Decl::Friend:430case Decl::FriendTemplate:431case Decl::StaticAssert:432case Decl::Block:433case Decl::Captured:434case Decl::Import:435case Decl::OMPThreadPrivate:436case Decl::OMPAllocate:437case Decl::OMPRequires:438case Decl::OMPCapturedExpr:439case Decl::OMPDeclareReduction:440case Decl::OMPDeclareMapper:441case Decl::BuiltinTemplate:442case Decl::Decomposition:443case Decl::Binding:444case Decl::Concept:445case Decl::ImplicitConceptSpecialization:446case Decl::LifetimeExtendedTemporary:447case Decl::RequiresExprBody:448case Decl::UnresolvedUsingIfExists:449case Decl::HLSLBuffer:450return false;451452// These indirectly derive from Redeclarable<T> but are not actually453// redeclarable.454case Decl::ImplicitParam:455case Decl::ParmVar:456case Decl::ObjCTypeParam:457return false;458}459460llvm_unreachable("Unhandled declaration kind");461}462463bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {464// Friend declarations in dependent contexts aren't anonymous in the usual465// sense, but they cannot be found by name lookup in their semantic context466// (or indeed in any context), so we treat them as anonymous.467//468// This doesn't apply to friend tag decls; Sema makes those available to name469// lookup in the surrounding context.470if (D->getFriendObjectKind() &&471D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {472// For function templates and class templates, the template is numbered and473// not its pattern.474if (auto *FD = dyn_cast<FunctionDecl>(D))475return !FD->getDescribedFunctionTemplate();476if (auto *RD = dyn_cast<CXXRecordDecl>(D))477return !RD->getDescribedClassTemplate();478return true;479}480481// At block scope, we number everything that we need to deduplicate, since we482// can't just use name matching to keep things lined up.483// FIXME: This is only necessary for an inline function or a template or484// similar.485if (D->getLexicalDeclContext()->isFunctionOrMethod()) {486if (auto *VD = dyn_cast<VarDecl>(D))487return VD->isStaticLocal();488// FIXME: What about CapturedDecls (and declarations nested within them)?489return isa<TagDecl, BlockDecl>(D);490}491492// Otherwise, we only care about anonymous class members / block-scope decls.493// FIXME: We need to handle blocks within inline / templated variables too.494if (D->getDeclName())495return false;496if (!isa<RecordDecl, ObjCInterfaceDecl>(D->getLexicalDeclContext()))497return false;498return isa<TagDecl, FieldDecl>(D);499}500501502