1*e5dd7070Spatrick //===- ASTCommon.h - Common stuff for ASTReader/ASTWriter -*- C++ -*-=========//
2*e5dd7070Spatrick //
3*e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e5dd7070Spatrick //
7*e5dd7070Spatrick //===----------------------------------------------------------------------===//
8*e5dd7070Spatrick //
9*e5dd7070Spatrick // This file defines common functions that both ASTReader and ASTWriter use.
10*e5dd7070Spatrick //
11*e5dd7070Spatrick //===----------------------------------------------------------------------===//
12*e5dd7070Spatrick
13*e5dd7070Spatrick #ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
14*e5dd7070Spatrick #define LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
15*e5dd7070Spatrick
16*e5dd7070Spatrick #include "clang/AST/ASTContext.h"
17*e5dd7070Spatrick #include "clang/AST/DeclFriend.h"
18*e5dd7070Spatrick #include "clang/Serialization/ASTBitCodes.h"
19*e5dd7070Spatrick
20*e5dd7070Spatrick namespace clang {
21*e5dd7070Spatrick
22*e5dd7070Spatrick namespace serialization {
23*e5dd7070Spatrick
24*e5dd7070Spatrick enum DeclUpdateKind {
25*e5dd7070Spatrick UPD_CXX_ADDED_IMPLICIT_MEMBER,
26*e5dd7070Spatrick UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
27*e5dd7070Spatrick UPD_CXX_ADDED_ANONYMOUS_NAMESPACE,
28*e5dd7070Spatrick UPD_CXX_ADDED_FUNCTION_DEFINITION,
29*e5dd7070Spatrick UPD_CXX_ADDED_VAR_DEFINITION,
30*e5dd7070Spatrick UPD_CXX_POINT_OF_INSTANTIATION,
31*e5dd7070Spatrick UPD_CXX_INSTANTIATED_CLASS_DEFINITION,
32*e5dd7070Spatrick UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT,
33*e5dd7070Spatrick UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER,
34*e5dd7070Spatrick UPD_CXX_RESOLVED_DTOR_DELETE,
35*e5dd7070Spatrick UPD_CXX_RESOLVED_EXCEPTION_SPEC,
36*e5dd7070Spatrick UPD_CXX_DEDUCED_RETURN_TYPE,
37*e5dd7070Spatrick UPD_DECL_MARKED_USED,
38*e5dd7070Spatrick UPD_MANGLING_NUMBER,
39*e5dd7070Spatrick UPD_STATIC_LOCAL_NUMBER,
40*e5dd7070Spatrick UPD_DECL_MARKED_OPENMP_THREADPRIVATE,
41*e5dd7070Spatrick UPD_DECL_MARKED_OPENMP_ALLOCATE,
42*e5dd7070Spatrick UPD_DECL_MARKED_OPENMP_DECLARETARGET,
43*e5dd7070Spatrick UPD_DECL_EXPORTED,
44*e5dd7070Spatrick UPD_ADDED_ATTR_TO_RECORD
45*e5dd7070Spatrick };
46*e5dd7070Spatrick
47*e5dd7070Spatrick TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT);
48*e5dd7070Spatrick
49*e5dd7070Spatrick template <typename IdxForTypeTy>
MakeTypeID(ASTContext & Context,QualType T,IdxForTypeTy IdxForType)50*e5dd7070Spatrick TypeID MakeTypeID(ASTContext &Context, QualType T, IdxForTypeTy IdxForType) {
51*e5dd7070Spatrick if (T.isNull())
52*e5dd7070Spatrick return PREDEF_TYPE_NULL_ID;
53*e5dd7070Spatrick
54*e5dd7070Spatrick unsigned FastQuals = T.getLocalFastQualifiers();
55*e5dd7070Spatrick T.removeLocalFastQualifiers();
56*e5dd7070Spatrick
57*e5dd7070Spatrick if (T.hasLocalNonFastQualifiers())
58*e5dd7070Spatrick return IdxForType(T).asTypeID(FastQuals);
59*e5dd7070Spatrick
60*e5dd7070Spatrick assert(!T.hasLocalQualifiers());
61*e5dd7070Spatrick
62*e5dd7070Spatrick if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr()))
63*e5dd7070Spatrick return TypeIdxFromBuiltin(BT).asTypeID(FastQuals);
64*e5dd7070Spatrick
65*e5dd7070Spatrick if (T == Context.AutoDeductTy)
66*e5dd7070Spatrick return TypeIdx(PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
67*e5dd7070Spatrick if (T == Context.AutoRRefDeductTy)
68*e5dd7070Spatrick return TypeIdx(PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
69*e5dd7070Spatrick
70*e5dd7070Spatrick return IdxForType(T).asTypeID(FastQuals);
71*e5dd7070Spatrick }
72*e5dd7070Spatrick
73*e5dd7070Spatrick unsigned ComputeHash(Selector Sel);
74*e5dd7070Spatrick
75*e5dd7070Spatrick /// Retrieve the "definitive" declaration that provides all of the
76*e5dd7070Spatrick /// visible entries for the given declaration context, if there is one.
77*e5dd7070Spatrick ///
78*e5dd7070Spatrick /// The "definitive" declaration is the only place where we need to look to
79*e5dd7070Spatrick /// find information about the declarations within the given declaration
80*e5dd7070Spatrick /// context. For example, C++ and Objective-C classes, C structs/unions, and
81*e5dd7070Spatrick /// Objective-C protocols, categories, and extensions are all defined in a
82*e5dd7070Spatrick /// single place in the source code, so they have definitive declarations
83*e5dd7070Spatrick /// associated with them. C++ namespaces, on the other hand, can have
84*e5dd7070Spatrick /// multiple definitions.
85*e5dd7070Spatrick const DeclContext *getDefinitiveDeclContext(const DeclContext *DC);
86*e5dd7070Spatrick
87*e5dd7070Spatrick /// Determine whether the given declaration kind is redeclarable.
88*e5dd7070Spatrick bool isRedeclarableDeclKind(unsigned Kind);
89*e5dd7070Spatrick
90*e5dd7070Spatrick /// Determine whether the given declaration needs an anonymous
91*e5dd7070Spatrick /// declaration number.
92*e5dd7070Spatrick bool needsAnonymousDeclarationNumber(const NamedDecl *D);
93*e5dd7070Spatrick
94*e5dd7070Spatrick /// Visit each declaration within \c DC that needs an anonymous
95*e5dd7070Spatrick /// declaration number and call \p Visit with the declaration and its number.
numberAnonymousDeclsWithin(const DeclContext * DC,Fn Visit)96*e5dd7070Spatrick template<typename Fn> void numberAnonymousDeclsWithin(const DeclContext *DC,
97*e5dd7070Spatrick Fn Visit) {
98*e5dd7070Spatrick unsigned Index = 0;
99*e5dd7070Spatrick for (Decl *LexicalD : DC->decls()) {
100*e5dd7070Spatrick // For a friend decl, we care about the declaration within it, if any.
101*e5dd7070Spatrick if (auto *FD = dyn_cast<FriendDecl>(LexicalD))
102*e5dd7070Spatrick LexicalD = FD->getFriendDecl();
103*e5dd7070Spatrick
104*e5dd7070Spatrick auto *ND = dyn_cast_or_null<NamedDecl>(LexicalD);
105*e5dd7070Spatrick if (!ND || !needsAnonymousDeclarationNumber(ND))
106*e5dd7070Spatrick continue;
107*e5dd7070Spatrick
108*e5dd7070Spatrick Visit(ND, Index++);
109*e5dd7070Spatrick }
110*e5dd7070Spatrick }
111*e5dd7070Spatrick
112*e5dd7070Spatrick /// Determine whether the given declaration will be included in the per-module
113*e5dd7070Spatrick /// initializer if it needs to be eagerly handed to the AST consumer. If so, we
114*e5dd7070Spatrick /// should not hand it to the consumer when deserializing it, nor include it in
115*e5dd7070Spatrick /// the list of eagerly deserialized declarations.
isPartOfPerModuleInitializer(const Decl * D)116*e5dd7070Spatrick inline bool isPartOfPerModuleInitializer(const Decl *D) {
117*e5dd7070Spatrick if (isa<ImportDecl>(D))
118*e5dd7070Spatrick return true;
119*e5dd7070Spatrick // Template instantiations are notionally in an "instantiation unit" rather
120*e5dd7070Spatrick // than in any particular translation unit, so they need not be part of any
121*e5dd7070Spatrick // particular (sub)module's per-module initializer.
122*e5dd7070Spatrick if (auto *VD = dyn_cast<VarDecl>(D))
123*e5dd7070Spatrick return !isTemplateInstantiation(VD->getTemplateSpecializationKind());
124*e5dd7070Spatrick return false;
125*e5dd7070Spatrick }
126*e5dd7070Spatrick
127*e5dd7070Spatrick } // namespace serialization
128*e5dd7070Spatrick
129*e5dd7070Spatrick } // namespace clang
130*e5dd7070Spatrick
131*e5dd7070Spatrick #endif
132