xref: /freebsd/contrib/llvm-project/clang/lib/Serialization/ASTCommon.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
21 namespace clang {
22 
23 namespace serialization {
24 
25 enum 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 };
46 
47 TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT);
48 
49 unsigned ComputeHash(Selector Sel);
50 
51 /// Retrieve the "definitive" declaration that provides all of the
52 /// visible entries for the given declaration context, if there is one.
53 ///
54 /// The "definitive" declaration is the only place where we need to look to
55 /// find information about the declarations within the given declaration
56 /// context. For example, C++ and Objective-C classes, C structs/unions, and
57 /// Objective-C protocols, categories, and extensions are all defined in a
58 /// single place in the source code, so they have definitive declarations
59 /// associated with them. C++ namespaces, on the other hand, can have
60 /// multiple definitions.
61 const DeclContext *getDefinitiveDeclContext(const DeclContext *DC);
62 
63 /// Determine whether the given declaration kind is redeclarable.
64 bool isRedeclarableDeclKind(unsigned Kind);
65 
66 /// Determine whether the given declaration needs an anonymous
67 /// declaration number.
68 bool needsAnonymousDeclarationNumber(const NamedDecl *D);
69 
70 /// Visit each declaration within \c DC that needs an anonymous
71 /// declaration number and call \p Visit with the declaration and its number.
numberAnonymousDeclsWithin(const DeclContext * DC,Fn Visit)72 template<typename Fn> void numberAnonymousDeclsWithin(const DeclContext *DC,
73                                                       Fn Visit) {
74   unsigned Index = 0;
75   for (Decl *LexicalD : DC->decls()) {
76     // For a friend decl, we care about the declaration within it, if any.
77     if (auto *FD = dyn_cast<FriendDecl>(LexicalD))
78       LexicalD = FD->getFriendDecl();
79 
80     auto *ND = dyn_cast_or_null<NamedDecl>(LexicalD);
81     if (!ND || !needsAnonymousDeclarationNumber(ND))
82       continue;
83 
84     Visit(ND, Index++);
85   }
86 }
87 
88 /// Determine whether the given declaration will be included in the per-module
89 /// initializer if it needs to be eagerly handed to the AST consumer. If so, we
90 /// should not hand it to the consumer when deserializing it, nor include it in
91 /// the list of eagerly deserialized declarations.
isPartOfPerModuleInitializer(const Decl * D)92 inline bool isPartOfPerModuleInitializer(const Decl *D) {
93   if (isa<ImportDecl>(D))
94     return true;
95   // Template instantiations are notionally in an "instantiation unit" rather
96   // than in any particular translation unit, so they need not be part of any
97   // particular (sub)module's per-module initializer.
98   if (auto *VD = dyn_cast<VarDecl>(D))
99     return !isTemplateInstantiation(VD->getTemplateSpecializationKind());
100   return false;
101 }
102 
103 } // namespace serialization
104 
105 } // namespace clang
106 
107 #endif
108