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 "clang/Serialization/ModuleFile.h"
19 #include "llvm/Support/DJB.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/raw_ostream.h"
23 using namespace clang
;
25 // Give ASTDeserializationListener's VTable a home.
26 ASTDeserializationListener::~ASTDeserializationListener() { }
28 serialization::TypeIdx
29 serialization::TypeIdxFromBuiltin(const BuiltinType
*BT
) {
31 switch (BT
->getKind()) {
32 case BuiltinType::Void
:
33 ID
= PREDEF_TYPE_VOID_ID
;
35 case BuiltinType::Bool
:
36 ID
= PREDEF_TYPE_BOOL_ID
;
38 case BuiltinType::Char_U
:
39 ID
= PREDEF_TYPE_CHAR_U_ID
;
41 case BuiltinType::UChar
:
42 ID
= PREDEF_TYPE_UCHAR_ID
;
44 case BuiltinType::UShort
:
45 ID
= PREDEF_TYPE_USHORT_ID
;
47 case BuiltinType::UInt
:
48 ID
= PREDEF_TYPE_UINT_ID
;
50 case BuiltinType::ULong
:
51 ID
= PREDEF_TYPE_ULONG_ID
;
53 case BuiltinType::ULongLong
:
54 ID
= PREDEF_TYPE_ULONGLONG_ID
;
56 case BuiltinType::UInt128
:
57 ID
= PREDEF_TYPE_UINT128_ID
;
59 case BuiltinType::Char_S
:
60 ID
= PREDEF_TYPE_CHAR_S_ID
;
62 case BuiltinType::SChar
:
63 ID
= PREDEF_TYPE_SCHAR_ID
;
65 case BuiltinType::WChar_S
:
66 case BuiltinType::WChar_U
:
67 ID
= PREDEF_TYPE_WCHAR_ID
;
69 case BuiltinType::Short
:
70 ID
= PREDEF_TYPE_SHORT_ID
;
72 case BuiltinType::Int
:
73 ID
= PREDEF_TYPE_INT_ID
;
75 case BuiltinType::Long
:
76 ID
= PREDEF_TYPE_LONG_ID
;
78 case BuiltinType::LongLong
:
79 ID
= PREDEF_TYPE_LONGLONG_ID
;
81 case BuiltinType::Int128
:
82 ID
= PREDEF_TYPE_INT128_ID
;
84 case BuiltinType::Half
:
85 ID
= PREDEF_TYPE_HALF_ID
;
87 case BuiltinType::Float
:
88 ID
= PREDEF_TYPE_FLOAT_ID
;
90 case BuiltinType::Double
:
91 ID
= PREDEF_TYPE_DOUBLE_ID
;
93 case BuiltinType::LongDouble
:
94 ID
= PREDEF_TYPE_LONGDOUBLE_ID
;
96 case BuiltinType::ShortAccum
:
97 ID
= PREDEF_TYPE_SHORT_ACCUM_ID
;
99 case BuiltinType::Accum
:
100 ID
= PREDEF_TYPE_ACCUM_ID
;
102 case BuiltinType::LongAccum
:
103 ID
= PREDEF_TYPE_LONG_ACCUM_ID
;
105 case BuiltinType::UShortAccum
:
106 ID
= PREDEF_TYPE_USHORT_ACCUM_ID
;
108 case BuiltinType::UAccum
:
109 ID
= PREDEF_TYPE_UACCUM_ID
;
111 case BuiltinType::ULongAccum
:
112 ID
= PREDEF_TYPE_ULONG_ACCUM_ID
;
114 case BuiltinType::ShortFract
:
115 ID
= PREDEF_TYPE_SHORT_FRACT_ID
;
117 case BuiltinType::Fract
:
118 ID
= PREDEF_TYPE_FRACT_ID
;
120 case BuiltinType::LongFract
:
121 ID
= PREDEF_TYPE_LONG_FRACT_ID
;
123 case BuiltinType::UShortFract
:
124 ID
= PREDEF_TYPE_USHORT_FRACT_ID
;
126 case BuiltinType::UFract
:
127 ID
= PREDEF_TYPE_UFRACT_ID
;
129 case BuiltinType::ULongFract
:
130 ID
= PREDEF_TYPE_ULONG_FRACT_ID
;
132 case BuiltinType::SatShortAccum
:
133 ID
= PREDEF_TYPE_SAT_SHORT_ACCUM_ID
;
135 case BuiltinType::SatAccum
:
136 ID
= PREDEF_TYPE_SAT_ACCUM_ID
;
138 case BuiltinType::SatLongAccum
:
139 ID
= PREDEF_TYPE_SAT_LONG_ACCUM_ID
;
141 case BuiltinType::SatUShortAccum
:
142 ID
= PREDEF_TYPE_SAT_USHORT_ACCUM_ID
;
144 case BuiltinType::SatUAccum
:
145 ID
= PREDEF_TYPE_SAT_UACCUM_ID
;
147 case BuiltinType::SatULongAccum
:
148 ID
= PREDEF_TYPE_SAT_ULONG_ACCUM_ID
;
150 case BuiltinType::SatShortFract
:
151 ID
= PREDEF_TYPE_SAT_SHORT_FRACT_ID
;
153 case BuiltinType::SatFract
:
154 ID
= PREDEF_TYPE_SAT_FRACT_ID
;
156 case BuiltinType::SatLongFract
:
157 ID
= PREDEF_TYPE_SAT_LONG_FRACT_ID
;
159 case BuiltinType::SatUShortFract
:
160 ID
= PREDEF_TYPE_SAT_USHORT_FRACT_ID
;
162 case BuiltinType::SatUFract
:
163 ID
= PREDEF_TYPE_SAT_UFRACT_ID
;
165 case BuiltinType::SatULongFract
:
166 ID
= PREDEF_TYPE_SAT_ULONG_FRACT_ID
;
168 case BuiltinType::Float16
:
169 ID
= PREDEF_TYPE_FLOAT16_ID
;
171 case BuiltinType::Float128
:
172 ID
= PREDEF_TYPE_FLOAT128_ID
;
174 case BuiltinType::Ibm128
:
175 ID
= PREDEF_TYPE_IBM128_ID
;
177 case BuiltinType::NullPtr
:
178 ID
= PREDEF_TYPE_NULLPTR_ID
;
180 case BuiltinType::Char8
:
181 ID
= PREDEF_TYPE_CHAR8_ID
;
183 case BuiltinType::Char16
:
184 ID
= PREDEF_TYPE_CHAR16_ID
;
186 case BuiltinType::Char32
:
187 ID
= PREDEF_TYPE_CHAR32_ID
;
189 case BuiltinType::Overload
:
190 ID
= PREDEF_TYPE_OVERLOAD_ID
;
192 case BuiltinType::UnresolvedTemplate
:
193 ID
= PREDEF_TYPE_UNRESOLVED_TEMPLATE
;
195 case BuiltinType::BoundMember
:
196 ID
= PREDEF_TYPE_BOUND_MEMBER
;
198 case BuiltinType::PseudoObject
:
199 ID
= PREDEF_TYPE_PSEUDO_OBJECT
;
201 case BuiltinType::Dependent
:
202 ID
= PREDEF_TYPE_DEPENDENT_ID
;
204 case BuiltinType::UnknownAny
:
205 ID
= PREDEF_TYPE_UNKNOWN_ANY
;
207 case BuiltinType::ARCUnbridgedCast
:
208 ID
= PREDEF_TYPE_ARC_UNBRIDGED_CAST
;
210 case BuiltinType::ObjCId
:
211 ID
= PREDEF_TYPE_OBJC_ID
;
213 case BuiltinType::ObjCClass
:
214 ID
= PREDEF_TYPE_OBJC_CLASS
;
216 case BuiltinType::ObjCSel
:
217 ID
= PREDEF_TYPE_OBJC_SEL
;
219 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
220 case BuiltinType::Id: \
221 ID = PREDEF_TYPE_##Id##_ID; \
223 #include "clang/Basic/OpenCLImageTypes.def"
224 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
225 case BuiltinType::Id: \
226 ID = PREDEF_TYPE_##Id##_ID; \
228 #include "clang/Basic/OpenCLExtensionTypes.def"
229 case BuiltinType::OCLSampler
:
230 ID
= PREDEF_TYPE_SAMPLER_ID
;
232 case BuiltinType::OCLEvent
:
233 ID
= PREDEF_TYPE_EVENT_ID
;
235 case BuiltinType::OCLClkEvent
:
236 ID
= PREDEF_TYPE_CLK_EVENT_ID
;
238 case BuiltinType::OCLQueue
:
239 ID
= PREDEF_TYPE_QUEUE_ID
;
241 case BuiltinType::OCLReserveID
:
242 ID
= PREDEF_TYPE_RESERVE_ID_ID
;
244 #define SVE_TYPE(Name, Id, SingletonId) \
245 case BuiltinType::Id: \
246 ID = PREDEF_TYPE_##Id##_ID; \
248 #include "clang/Basic/AArch64SVEACLETypes.def"
249 #define PPC_VECTOR_TYPE(Name, Id, Size) \
250 case BuiltinType::Id: \
251 ID = PREDEF_TYPE_##Id##_ID; \
253 #include "clang/Basic/PPCTypes.def"
254 #define RVV_TYPE(Name, Id, SingletonId) \
255 case BuiltinType::Id: \
256 ID = PREDEF_TYPE_##Id##_ID; \
258 #include "clang/Basic/RISCVVTypes.def"
259 #define WASM_TYPE(Name, Id, SingletonId) \
260 case BuiltinType::Id: \
261 ID = PREDEF_TYPE_##Id##_ID; \
263 #include "clang/Basic/WebAssemblyReferenceTypes.def"
264 #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
265 case BuiltinType::Id: \
266 ID = PREDEF_TYPE_##Id##_ID; \
268 #include "clang/Basic/AMDGPUTypes.def"
269 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
270 case BuiltinType::Id: \
271 ID = PREDEF_TYPE_##Id##_ID; \
273 #include "clang/Basic/HLSLIntangibleTypes.def"
274 case BuiltinType::BuiltinFn
:
275 ID
= PREDEF_TYPE_BUILTIN_FN
;
277 case BuiltinType::IncompleteMatrixIdx
:
278 ID
= PREDEF_TYPE_INCOMPLETE_MATRIX_IDX
;
280 case BuiltinType::ArraySection
:
281 ID
= PREDEF_TYPE_ARRAY_SECTION
;
283 case BuiltinType::OMPArrayShaping
:
284 ID
= PREDEF_TYPE_OMP_ARRAY_SHAPING
;
286 case BuiltinType::OMPIterator
:
287 ID
= PREDEF_TYPE_OMP_ITERATOR
;
289 case BuiltinType::BFloat16
:
290 ID
= PREDEF_TYPE_BFLOAT16_ID
;
294 return TypeIdx(0, ID
);
297 unsigned serialization::ComputeHash(Selector Sel
) {
298 unsigned N
= Sel
.getNumArgs();
302 for (unsigned I
= 0; I
!= N
; ++I
)
303 if (const IdentifierInfo
*II
= Sel
.getIdentifierInfoForSlot(I
))
304 R
= llvm::djbHash(II
->getName(), R
);
309 serialization::getDefinitiveDeclContext(const DeclContext
*DC
) {
310 switch (DC
->getDeclKind()) {
311 // These entities may have multiple definitions.
312 case Decl::TranslationUnit
:
313 case Decl::ExternCContext
:
314 case Decl::Namespace
:
315 case Decl::LinkageSpec
:
319 // C/C++ tag types can only be defined in one place.
322 if (const TagDecl
*Def
= cast
<TagDecl
>(DC
)->getDefinition())
326 // FIXME: These can be defined in one place... except special member
327 // functions and out-of-line definitions.
328 case Decl::CXXRecord
:
329 case Decl::ClassTemplateSpecialization
:
330 case Decl::ClassTemplatePartialSpecialization
:
333 // Each function, method, and block declaration is its own DeclContext.
335 case Decl::CXXMethod
:
336 case Decl::CXXConstructor
:
337 case Decl::CXXDestructor
:
338 case Decl::CXXConversion
:
339 case Decl::ObjCMethod
:
342 // Objective C categories, category implementations, and class
343 // implementations can only be defined in one place.
344 case Decl::ObjCCategory
:
345 case Decl::ObjCCategoryImpl
:
346 case Decl::ObjCImplementation
:
349 case Decl::ObjCProtocol
:
350 if (const ObjCProtocolDecl
*Def
351 = cast
<ObjCProtocolDecl
>(DC
)->getDefinition())
355 // FIXME: These are defined in one place, but properties in class extensions
356 // end up being back-patched into the main interface. See
357 // SemaObjC::HandlePropertyInClassExtension for the offending code.
358 case Decl::ObjCInterface
:
362 llvm_unreachable("Unhandled DeclContext in AST reader");
365 llvm_unreachable("Unhandled decl kind");
368 bool serialization::isRedeclarableDeclKind(unsigned Kind
) {
369 switch (static_cast<Decl::Kind
>(Kind
)) {
370 case Decl::TranslationUnit
:
371 case Decl::ExternCContext
:
372 // Special case of a "merged" declaration.
375 case Decl::Namespace
:
376 case Decl::NamespaceAlias
:
378 case Decl::TypeAlias
:
381 case Decl::CXXRecord
:
382 case Decl::ClassTemplateSpecialization
:
383 case Decl::ClassTemplatePartialSpecialization
:
384 case Decl::VarTemplateSpecialization
:
385 case Decl::VarTemplatePartialSpecialization
:
387 case Decl::CXXDeductionGuide
:
388 case Decl::CXXMethod
:
389 case Decl::CXXConstructor
:
390 case Decl::CXXDestructor
:
391 case Decl::CXXConversion
:
392 case Decl::UsingShadow
:
393 case Decl::ConstructorUsingShadow
:
395 case Decl::FunctionTemplate
:
396 case Decl::ClassTemplate
:
397 case Decl::VarTemplate
:
398 case Decl::TypeAliasTemplate
:
399 case Decl::ObjCProtocol
:
400 case Decl::ObjCInterface
:
404 // Never redeclarable.
405 case Decl::UsingDirective
:
407 case Decl::UnresolvedUsingTypename
:
408 case Decl::TemplateTypeParm
:
409 case Decl::EnumConstant
:
410 case Decl::UnresolvedUsingValue
:
411 case Decl::IndirectField
:
413 case Decl::MSProperty
:
415 case Decl::UnnamedGlobalConstant
:
416 case Decl::TemplateParamObject
:
418 case Decl::ObjCAtDefsField
:
419 case Decl::NonTypeTemplateParm
:
420 case Decl::TemplateTemplateParm
:
422 case Decl::UsingEnum
:
423 case Decl::UsingPack
:
424 case Decl::ObjCMethod
:
425 case Decl::ObjCCategory
:
426 case Decl::ObjCCategoryImpl
:
427 case Decl::ObjCImplementation
:
428 case Decl::ObjCProperty
:
429 case Decl::ObjCCompatibleAlias
:
430 case Decl::LinkageSpec
:
432 case Decl::ObjCPropertyImpl
:
433 case Decl::PragmaComment
:
434 case Decl::PragmaDetectMismatch
:
435 case Decl::FileScopeAsm
:
436 case Decl::TopLevelStmt
:
437 case Decl::AccessSpec
:
439 case Decl::FriendTemplate
:
440 case Decl::StaticAssert
:
444 case Decl::OMPThreadPrivate
:
445 case Decl::OMPAllocate
:
446 case Decl::OMPRequires
:
447 case Decl::OMPCapturedExpr
:
448 case Decl::OMPDeclareReduction
:
449 case Decl::OMPDeclareMapper
:
450 case Decl::BuiltinTemplate
:
451 case Decl::Decomposition
:
454 case Decl::ImplicitConceptSpecialization
:
455 case Decl::LifetimeExtendedTemporary
:
456 case Decl::RequiresExprBody
:
457 case Decl::UnresolvedUsingIfExists
:
458 case Decl::HLSLBuffer
:
461 // These indirectly derive from Redeclarable<T> but are not actually
463 case Decl::ImplicitParam
:
465 case Decl::ObjCTypeParam
:
469 llvm_unreachable("Unhandled declaration kind");
472 bool serialization::needsAnonymousDeclarationNumber(const NamedDecl
*D
) {
473 // Friend declarations in dependent contexts aren't anonymous in the usual
474 // sense, but they cannot be found by name lookup in their semantic context
475 // (or indeed in any context), so we treat them as anonymous.
477 // This doesn't apply to friend tag decls; Sema makes those available to name
478 // lookup in the surrounding context.
479 if (D
->getFriendObjectKind() &&
480 D
->getLexicalDeclContext()->isDependentContext() && !isa
<TagDecl
>(D
)) {
481 // For function templates and class templates, the template is numbered and
483 if (auto *FD
= dyn_cast
<FunctionDecl
>(D
))
484 return !FD
->getDescribedFunctionTemplate();
485 if (auto *RD
= dyn_cast
<CXXRecordDecl
>(D
))
486 return !RD
->getDescribedClassTemplate();
490 // At block scope, we number everything that we need to deduplicate, since we
491 // can't just use name matching to keep things lined up.
492 // FIXME: This is only necessary for an inline function or a template or
494 if (D
->getLexicalDeclContext()->isFunctionOrMethod()) {
495 if (auto *VD
= dyn_cast
<VarDecl
>(D
))
496 return VD
->isStaticLocal();
497 // FIXME: What about CapturedDecls (and declarations nested within them)?
498 return isa
<TagDecl
, BlockDecl
>(D
);
501 // Otherwise, we only care about anonymous class members / block-scope decls.
502 // FIXME: We need to handle blocks within inline / templated variables too.
503 if (D
->getDeclName())
505 if (!isa
<RecordDecl
, ObjCInterfaceDecl
>(D
->getLexicalDeclContext()))
507 return isa
<TagDecl
, FieldDecl
>(D
);
510 void serialization::updateModuleTimestamp(StringRef ModuleFilename
) {
511 // Overwrite the timestamp file contents so that file's mtime changes.
513 llvm::raw_fd_ostream
OS(ModuleFile::getTimestampFilename(ModuleFilename
), EC
,
514 llvm::sys::fs::OF_TextWithCRLF
);
517 OS
<< "Timestamp file\n";
519 OS
.clear_error(); // Avoid triggering a fatal error.