1 //===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines common functions that both ASTReader and ASTWriter use.
11 //===----------------------------------------------------------------------===//
13 #include "ASTCommon.h"
14 #include "clang/AST/DeclCXX.h"
15 #include "clang/AST/DeclObjC.h"
16 #include "clang/Basic/IdentifierTable.h"
17 #include "clang/Serialization/ASTDeserializationListener.h"
18 #include "llvm/Support/DJB.h"
20 using namespace clang
;
22 // Give ASTDeserializationListener's VTable a home.
23 ASTDeserializationListener::~ASTDeserializationListener() { }
25 serialization::TypeIdx
26 serialization::TypeIdxFromBuiltin(const BuiltinType
*BT
) {
28 switch (BT
->getKind()) {
29 case BuiltinType::Void
:
30 ID
= PREDEF_TYPE_VOID_ID
;
32 case BuiltinType::Bool
:
33 ID
= PREDEF_TYPE_BOOL_ID
;
35 case BuiltinType::Char_U
:
36 ID
= PREDEF_TYPE_CHAR_U_ID
;
38 case BuiltinType::UChar
:
39 ID
= PREDEF_TYPE_UCHAR_ID
;
41 case BuiltinType::UShort
:
42 ID
= PREDEF_TYPE_USHORT_ID
;
44 case BuiltinType::UInt
:
45 ID
= PREDEF_TYPE_UINT_ID
;
47 case BuiltinType::ULong
:
48 ID
= PREDEF_TYPE_ULONG_ID
;
50 case BuiltinType::ULongLong
:
51 ID
= PREDEF_TYPE_ULONGLONG_ID
;
53 case BuiltinType::UInt128
:
54 ID
= PREDEF_TYPE_UINT128_ID
;
56 case BuiltinType::Char_S
:
57 ID
= PREDEF_TYPE_CHAR_S_ID
;
59 case BuiltinType::SChar
:
60 ID
= PREDEF_TYPE_SCHAR_ID
;
62 case BuiltinType::WChar_S
:
63 case BuiltinType::WChar_U
:
64 ID
= PREDEF_TYPE_WCHAR_ID
;
66 case BuiltinType::Short
:
67 ID
= PREDEF_TYPE_SHORT_ID
;
69 case BuiltinType::Int
:
70 ID
= PREDEF_TYPE_INT_ID
;
72 case BuiltinType::Long
:
73 ID
= PREDEF_TYPE_LONG_ID
;
75 case BuiltinType::LongLong
:
76 ID
= PREDEF_TYPE_LONGLONG_ID
;
78 case BuiltinType::Int128
:
79 ID
= PREDEF_TYPE_INT128_ID
;
81 case BuiltinType::Half
:
82 ID
= PREDEF_TYPE_HALF_ID
;
84 case BuiltinType::Float
:
85 ID
= PREDEF_TYPE_FLOAT_ID
;
87 case BuiltinType::Double
:
88 ID
= PREDEF_TYPE_DOUBLE_ID
;
90 case BuiltinType::LongDouble
:
91 ID
= PREDEF_TYPE_LONGDOUBLE_ID
;
93 case BuiltinType::ShortAccum
:
94 ID
= PREDEF_TYPE_SHORT_ACCUM_ID
;
96 case BuiltinType::Accum
:
97 ID
= PREDEF_TYPE_ACCUM_ID
;
99 case BuiltinType::LongAccum
:
100 ID
= PREDEF_TYPE_LONG_ACCUM_ID
;
102 case BuiltinType::UShortAccum
:
103 ID
= PREDEF_TYPE_USHORT_ACCUM_ID
;
105 case BuiltinType::UAccum
:
106 ID
= PREDEF_TYPE_UACCUM_ID
;
108 case BuiltinType::ULongAccum
:
109 ID
= PREDEF_TYPE_ULONG_ACCUM_ID
;
111 case BuiltinType::ShortFract
:
112 ID
= PREDEF_TYPE_SHORT_FRACT_ID
;
114 case BuiltinType::Fract
:
115 ID
= PREDEF_TYPE_FRACT_ID
;
117 case BuiltinType::LongFract
:
118 ID
= PREDEF_TYPE_LONG_FRACT_ID
;
120 case BuiltinType::UShortFract
:
121 ID
= PREDEF_TYPE_USHORT_FRACT_ID
;
123 case BuiltinType::UFract
:
124 ID
= PREDEF_TYPE_UFRACT_ID
;
126 case BuiltinType::ULongFract
:
127 ID
= PREDEF_TYPE_ULONG_FRACT_ID
;
129 case BuiltinType::SatShortAccum
:
130 ID
= PREDEF_TYPE_SAT_SHORT_ACCUM_ID
;
132 case BuiltinType::SatAccum
:
133 ID
= PREDEF_TYPE_SAT_ACCUM_ID
;
135 case BuiltinType::SatLongAccum
:
136 ID
= PREDEF_TYPE_SAT_LONG_ACCUM_ID
;
138 case BuiltinType::SatUShortAccum
:
139 ID
= PREDEF_TYPE_SAT_USHORT_ACCUM_ID
;
141 case BuiltinType::SatUAccum
:
142 ID
= PREDEF_TYPE_SAT_UACCUM_ID
;
144 case BuiltinType::SatULongAccum
:
145 ID
= PREDEF_TYPE_SAT_ULONG_ACCUM_ID
;
147 case BuiltinType::SatShortFract
:
148 ID
= PREDEF_TYPE_SAT_SHORT_FRACT_ID
;
150 case BuiltinType::SatFract
:
151 ID
= PREDEF_TYPE_SAT_FRACT_ID
;
153 case BuiltinType::SatLongFract
:
154 ID
= PREDEF_TYPE_SAT_LONG_FRACT_ID
;
156 case BuiltinType::SatUShortFract
:
157 ID
= PREDEF_TYPE_SAT_USHORT_FRACT_ID
;
159 case BuiltinType::SatUFract
:
160 ID
= PREDEF_TYPE_SAT_UFRACT_ID
;
162 case BuiltinType::SatULongFract
:
163 ID
= PREDEF_TYPE_SAT_ULONG_FRACT_ID
;
165 case BuiltinType::Float16
:
166 ID
= PREDEF_TYPE_FLOAT16_ID
;
168 case BuiltinType::Float128
:
169 ID
= PREDEF_TYPE_FLOAT128_ID
;
171 case BuiltinType::Ibm128
:
172 ID
= PREDEF_TYPE_IBM128_ID
;
174 case BuiltinType::NullPtr
:
175 ID
= PREDEF_TYPE_NULLPTR_ID
;
177 case BuiltinType::Char8
:
178 ID
= PREDEF_TYPE_CHAR8_ID
;
180 case BuiltinType::Char16
:
181 ID
= PREDEF_TYPE_CHAR16_ID
;
183 case BuiltinType::Char32
:
184 ID
= PREDEF_TYPE_CHAR32_ID
;
186 case BuiltinType::Overload
:
187 ID
= PREDEF_TYPE_OVERLOAD_ID
;
189 case BuiltinType::BoundMember
:
190 ID
= PREDEF_TYPE_BOUND_MEMBER
;
192 case BuiltinType::PseudoObject
:
193 ID
= PREDEF_TYPE_PSEUDO_OBJECT
;
195 case BuiltinType::Dependent
:
196 ID
= PREDEF_TYPE_DEPENDENT_ID
;
198 case BuiltinType::UnknownAny
:
199 ID
= PREDEF_TYPE_UNKNOWN_ANY
;
201 case BuiltinType::ARCUnbridgedCast
:
202 ID
= PREDEF_TYPE_ARC_UNBRIDGED_CAST
;
204 case BuiltinType::ObjCId
:
205 ID
= PREDEF_TYPE_OBJC_ID
;
207 case BuiltinType::ObjCClass
:
208 ID
= PREDEF_TYPE_OBJC_CLASS
;
210 case BuiltinType::ObjCSel
:
211 ID
= PREDEF_TYPE_OBJC_SEL
;
213 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
214 case BuiltinType::Id: \
215 ID = PREDEF_TYPE_##Id##_ID; \
217 #include "clang/Basic/OpenCLImageTypes.def"
218 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
219 case BuiltinType::Id: \
220 ID = PREDEF_TYPE_##Id##_ID; \
222 #include "clang/Basic/OpenCLExtensionTypes.def"
223 case BuiltinType::OCLSampler
:
224 ID
= PREDEF_TYPE_SAMPLER_ID
;
226 case BuiltinType::OCLEvent
:
227 ID
= PREDEF_TYPE_EVENT_ID
;
229 case BuiltinType::OCLClkEvent
:
230 ID
= PREDEF_TYPE_CLK_EVENT_ID
;
232 case BuiltinType::OCLQueue
:
233 ID
= PREDEF_TYPE_QUEUE_ID
;
235 case BuiltinType::OCLReserveID
:
236 ID
= PREDEF_TYPE_RESERVE_ID_ID
;
238 #define SVE_TYPE(Name, Id, SingletonId) \
239 case BuiltinType::Id: \
240 ID = PREDEF_TYPE_##Id##_ID; \
242 #include "clang/Basic/AArch64SVEACLETypes.def"
243 #define PPC_VECTOR_TYPE(Name, Id, Size) \
244 case BuiltinType::Id: \
245 ID = PREDEF_TYPE_##Id##_ID; \
247 #include "clang/Basic/PPCTypes.def"
248 #define RVV_TYPE(Name, Id, SingletonId) \
249 case BuiltinType::Id: \
250 ID = PREDEF_TYPE_##Id##_ID; \
252 #include "clang/Basic/RISCVVTypes.def"
253 #define WASM_TYPE(Name, Id, SingletonId) \
254 case BuiltinType::Id: \
255 ID = PREDEF_TYPE_##Id##_ID; \
257 #include "clang/Basic/WebAssemblyReferenceTypes.def"
258 case BuiltinType::BuiltinFn
:
259 ID
= PREDEF_TYPE_BUILTIN_FN
;
261 case BuiltinType::IncompleteMatrixIdx
:
262 ID
= PREDEF_TYPE_INCOMPLETE_MATRIX_IDX
;
264 case BuiltinType::OMPArraySection
:
265 ID
= PREDEF_TYPE_OMP_ARRAY_SECTION
;
267 case BuiltinType::OMPArrayShaping
:
268 ID
= PREDEF_TYPE_OMP_ARRAY_SHAPING
;
270 case BuiltinType::OMPIterator
:
271 ID
= PREDEF_TYPE_OMP_ITERATOR
;
273 case BuiltinType::BFloat16
:
274 ID
= PREDEF_TYPE_BFLOAT16_ID
;
281 unsigned serialization::ComputeHash(Selector Sel
) {
282 unsigned N
= Sel
.getNumArgs();
286 for (unsigned I
= 0; I
!= N
; ++I
)
287 if (IdentifierInfo
*II
= Sel
.getIdentifierInfoForSlot(I
))
288 R
= llvm::djbHash(II
->getName(), R
);
293 serialization::getDefinitiveDeclContext(const DeclContext
*DC
) {
294 switch (DC
->getDeclKind()) {
295 // These entities may have multiple definitions.
296 case Decl::TranslationUnit
:
297 case Decl::ExternCContext
:
298 case Decl::Namespace
:
299 case Decl::LinkageSpec
:
303 // C/C++ tag types can only be defined in one place.
306 if (const TagDecl
*Def
= cast
<TagDecl
>(DC
)->getDefinition())
310 // FIXME: These can be defined in one place... except special member
311 // functions and out-of-line definitions.
312 case Decl::CXXRecord
:
313 case Decl::ClassTemplateSpecialization
:
314 case Decl::ClassTemplatePartialSpecialization
:
317 // Each function, method, and block declaration is its own DeclContext.
319 case Decl::CXXMethod
:
320 case Decl::CXXConstructor
:
321 case Decl::CXXDestructor
:
322 case Decl::CXXConversion
:
323 case Decl::ObjCMethod
:
326 // Objective C categories, category implementations, and class
327 // implementations can only be defined in one place.
328 case Decl::ObjCCategory
:
329 case Decl::ObjCCategoryImpl
:
330 case Decl::ObjCImplementation
:
333 case Decl::ObjCProtocol
:
334 if (const ObjCProtocolDecl
*Def
335 = cast
<ObjCProtocolDecl
>(DC
)->getDefinition())
339 // FIXME: These are defined in one place, but properties in class extensions
340 // end up being back-patched into the main interface. See
341 // Sema::HandlePropertyInClassExtension for the offending code.
342 case Decl::ObjCInterface
:
346 llvm_unreachable("Unhandled DeclContext in AST reader");
349 llvm_unreachable("Unhandled decl kind");
352 bool serialization::isRedeclarableDeclKind(unsigned Kind
) {
353 switch (static_cast<Decl::Kind
>(Kind
)) {
354 case Decl::TranslationUnit
:
355 case Decl::ExternCContext
:
356 // Special case of a "merged" declaration.
359 case Decl::Namespace
:
360 case Decl::NamespaceAlias
:
362 case Decl::TypeAlias
:
365 case Decl::CXXRecord
:
366 case Decl::ClassTemplateSpecialization
:
367 case Decl::ClassTemplatePartialSpecialization
:
368 case Decl::VarTemplateSpecialization
:
369 case Decl::VarTemplatePartialSpecialization
:
371 case Decl::CXXDeductionGuide
:
372 case Decl::CXXMethod
:
373 case Decl::CXXConstructor
:
374 case Decl::CXXDestructor
:
375 case Decl::CXXConversion
:
376 case Decl::UsingShadow
:
377 case Decl::ConstructorUsingShadow
:
379 case Decl::FunctionTemplate
:
380 case Decl::ClassTemplate
:
381 case Decl::VarTemplate
:
382 case Decl::TypeAliasTemplate
:
383 case Decl::ObjCProtocol
:
384 case Decl::ObjCInterface
:
388 // Never redeclarable.
389 case Decl::UsingDirective
:
391 case Decl::UnresolvedUsingTypename
:
392 case Decl::TemplateTypeParm
:
393 case Decl::EnumConstant
:
394 case Decl::UnresolvedUsingValue
:
395 case Decl::IndirectField
:
397 case Decl::MSProperty
:
399 case Decl::UnnamedGlobalConstant
:
400 case Decl::TemplateParamObject
:
402 case Decl::ObjCAtDefsField
:
403 case Decl::NonTypeTemplateParm
:
404 case Decl::TemplateTemplateParm
:
406 case Decl::UsingEnum
:
407 case Decl::UsingPack
:
408 case Decl::ObjCMethod
:
409 case Decl::ObjCCategory
:
410 case Decl::ObjCCategoryImpl
:
411 case Decl::ObjCImplementation
:
412 case Decl::ObjCProperty
:
413 case Decl::ObjCCompatibleAlias
:
414 case Decl::LinkageSpec
:
416 case Decl::ObjCPropertyImpl
:
417 case Decl::PragmaComment
:
418 case Decl::PragmaDetectMismatch
:
419 case Decl::FileScopeAsm
:
420 case Decl::TopLevelStmt
:
421 case Decl::AccessSpec
:
423 case Decl::FriendTemplate
:
424 case Decl::StaticAssert
:
427 case Decl::ClassScopeFunctionSpecialization
:
429 case Decl::OMPThreadPrivate
:
430 case Decl::OMPAllocate
:
431 case Decl::OMPRequires
:
432 case Decl::OMPCapturedExpr
:
433 case Decl::OMPDeclareReduction
:
434 case Decl::OMPDeclareMapper
:
435 case Decl::BuiltinTemplate
:
436 case Decl::Decomposition
:
439 case Decl::ImplicitConceptSpecialization
:
440 case Decl::LifetimeExtendedTemporary
:
441 case Decl::RequiresExprBody
:
442 case Decl::UnresolvedUsingIfExists
:
443 case Decl::HLSLBuffer
:
446 // These indirectly derive from Redeclarable<T> but are not actually
448 case Decl::ImplicitParam
:
450 case Decl::ObjCTypeParam
:
454 llvm_unreachable("Unhandled declaration kind");
457 bool serialization::needsAnonymousDeclarationNumber(const NamedDecl
*D
) {
458 // Friend declarations in dependent contexts aren't anonymous in the usual
459 // sense, but they cannot be found by name lookup in their semantic context
460 // (or indeed in any context), so we treat them as anonymous.
462 // This doesn't apply to friend tag decls; Sema makes those available to name
463 // lookup in the surrounding context.
464 if (D
->getFriendObjectKind() &&
465 D
->getLexicalDeclContext()->isDependentContext() && !isa
<TagDecl
>(D
)) {
466 // For function templates and class templates, the template is numbered and
468 if (auto *FD
= dyn_cast
<FunctionDecl
>(D
))
469 return !FD
->getDescribedFunctionTemplate();
470 if (auto *RD
= dyn_cast
<CXXRecordDecl
>(D
))
471 return !RD
->getDescribedClassTemplate();
475 // At block scope, we number everything that we need to deduplicate, since we
476 // can't just use name matching to keep things lined up.
477 // FIXME: This is only necessary for an inline function or a template or
479 if (D
->getLexicalDeclContext()->isFunctionOrMethod()) {
480 if (auto *VD
= dyn_cast
<VarDecl
>(D
))
481 return VD
->isStaticLocal();
482 // FIXME: What about CapturedDecls (and declarations nested within them)?
483 return isa
<TagDecl
, BlockDecl
>(D
);
486 // Otherwise, we only care about anonymous class members / block-scope decls.
487 // FIXME: We need to handle blocks within inline / templated variables too.
488 if (D
->getDeclName())
490 if (!isa
<RecordDecl
, ObjCInterfaceDecl
>(D
->getLexicalDeclContext()))
492 return isa
<TagDecl
, FieldDecl
>(D
);