1//===- ASTCommon.h - Common stuff for ASTReader/ASTWriter -*- C++ -*-=========//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines common functions that both ASTReader and ASTWriter use.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
14#define LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
15
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclFriend.h"
18#include "clang/Basic/LLVM.h"
19#include "clang/Serialization/ASTBitCodes.h"
20
21namespace clang {
22
23namespace serialization {
24
25enum class DeclUpdateKind {
26 CXXAddedImplicitMember,
27 CXXAddedAnonymousNamespace,
28 CXXAddedFunctionDefinition,
29 CXXAddedVarDefinition,
30 CXXPointOfInstantiation,
31 CXXInstantiatedClassDefinition,
32 CXXInstantiatedDefaultArgument,
33 CXXInstantiatedDefaultMemberInitializer,
34 CXXResolvedDtorDelete,
35 CXXResolvedExceptionSpec,
36 CXXDeducedReturnType,
37 DeclMarkedUsed,
38 ManglingNumber,
39 StaticLocalNumber,
40 DeclMarkedOpenMPThreadPrivate,
41 DeclMarkedOpenMPAllocate,
42 DeclMarkedOpenMPDeclareTarget,
43 DeclExported,
44 AddedAttrToRecord,
45 CXXResolvedDtorGlobDelete,
46 CXXResolvedDtorArrayDelete,
47 CXXResolvedDtorGlobArrayDelete
48};
49
50TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT);
51
52unsigned ComputeHash(Selector Sel);
53
54/// Retrieve the "definitive" declaration that provides all of the
55/// visible entries for the given declaration context, if there is one.
56///
57/// The "definitive" declaration is the only place where we need to look to
58/// find information about the declarations within the given declaration
59/// context. For example, C++ and Objective-C classes, C structs/unions, and
60/// Objective-C protocols, categories, and extensions are all defined in a
61/// single place in the source code, so they have definitive declarations
62/// associated with them. C++ namespaces, on the other hand, can have
63/// multiple definitions.
64const DeclContext *getDefinitiveDeclContext(const DeclContext *DC);
65
66/// Determine whether the given declaration kind is redeclarable.
67bool isRedeclarableDeclKind(unsigned Kind);
68
69/// Determine whether the given declaration needs an anonymous
70/// declaration number.
71bool needsAnonymousDeclarationNumber(const NamedDecl *D);
72
73/// Visit each declaration within \c DC that needs an anonymous
74/// declaration number and call \p Visit with the declaration and its number.
75template<typename Fn> void numberAnonymousDeclsWithin(const DeclContext *DC,
76 Fn Visit) {
77 unsigned Index = 0;
78 for (Decl *LexicalD : DC->decls()) {
79 // For a friend decl, we care about the declaration within it, if any.
80 if (auto *FD = dyn_cast<FriendDecl>(Val: LexicalD))
81 LexicalD = FD->getFriendDecl();
82
83 auto *ND = dyn_cast_or_null<NamedDecl>(Val: LexicalD);
84 if (!ND || !needsAnonymousDeclarationNumber(D: ND))
85 continue;
86
87 Visit(ND, Index++);
88 }
89}
90
91/// Determine whether the given declaration will be included in the per-module
92/// initializer if it needs to be eagerly handed to the AST consumer. If so, we
93/// should not hand it to the consumer when deserializing it, nor include it in
94/// the list of eagerly deserialized declarations.
95inline bool isPartOfPerModuleInitializer(const Decl *D) {
96 if (isa<ImportDecl>(Val: D))
97 return true;
98 // Template instantiations are notionally in an "instantiation unit" rather
99 // than in any particular translation unit, so they need not be part of any
100 // particular (sub)module's per-module initializer.
101 if (auto *VD = dyn_cast<VarDecl>(Val: D))
102 return !isTemplateInstantiation(Kind: VD->getTemplateSpecializationKind());
103 return false;
104}
105
106} // namespace serialization
107
108} // namespace clang
109
110#endif
111