1 //===- ASTCommon.h - Common stuff for ASTReader/ASTWriter -*- C++ -*-=========//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines common functions that both ASTReader and ASTWriter use.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
15 #define LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
16 
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclFriend.h"
19 #include "clang/Serialization/ASTBitCodes.h"
20 
21 namespace clang {
22 
23 namespace serialization {
24 
25 enum DeclUpdateKind {
26   UPD_CXX_ADDED_IMPLICIT_MEMBER,
27   UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
28   UPD_CXX_ADDED_ANONYMOUS_NAMESPACE,
29   UPD_CXX_ADDED_FUNCTION_DEFINITION,
30   UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
31   UPD_CXX_INSTANTIATED_CLASS_DEFINITION,
32   UPD_CXX_RESOLVED_EXCEPTION_SPEC,
33   UPD_CXX_DEDUCED_RETURN_TYPE,
34   UPD_DECL_MARKED_USED,
35   UPD_MANGLING_NUMBER,
36   UPD_STATIC_LOCAL_NUMBER,
37   UPD_DECL_MARKED_OPENMP_THREADPRIVATE
38 };
39 
40 TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT);
41 
42 template <typename IdxForTypeTy>
43 TypeID MakeTypeID(ASTContext &Context, QualType T, IdxForTypeTy IdxForType) {
44   if (T.isNull())
45     return PREDEF_TYPE_NULL_ID;
46 
47   unsigned FastQuals = T.getLocalFastQualifiers();
48   T.removeLocalFastQualifiers();
49 
50   if (T.hasLocalNonFastQualifiers())
51     return IdxForType(T).asTypeID(FastQuals);
52 
53   assert(!T.hasLocalQualifiers());
54 
55   if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr()))
56     return TypeIdxFromBuiltin(BT).asTypeID(FastQuals);
57 
58   if (T == Context.AutoDeductTy)
59     return TypeIdx(PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
60   if (T == Context.AutoRRefDeductTy)
61     return TypeIdx(PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
62   if (T == Context.VaListTagTy)
63     return TypeIdx(PREDEF_TYPE_VA_LIST_TAG).asTypeID(FastQuals);
64 
65   return IdxForType(T).asTypeID(FastQuals);
66 }
67 
68 unsigned ComputeHash(Selector Sel);
69 
70 /// \brief Retrieve the "definitive" declaration that provides all of the
71 /// visible entries for the given declaration context, if there is one.
72 ///
73 /// The "definitive" declaration is the only place where we need to look to
74 /// find information about the declarations within the given declaration
75 /// context. For example, C++ and Objective-C classes, C structs/unions, and
76 /// Objective-C protocols, categories, and extensions are all defined in a
77 /// single place in the source code, so they have definitive declarations
78 /// associated with them. C++ namespaces, on the other hand, can have
79 /// multiple definitions.
80 const DeclContext *getDefinitiveDeclContext(const DeclContext *DC);
81 
82 /// \brief Determine whether the given declaration kind is redeclarable.
83 bool isRedeclarableDeclKind(unsigned Kind);
84 
85 /// \brief Determine whether the given declaration needs an anonymous
86 /// declaration number.
87 bool needsAnonymousDeclarationNumber(const NamedDecl *D);
88 
89 /// \brief Visit each declaration within \c DC that needs an anonymous
90 /// declaration number and call \p Visit with the declaration and its number.
91 template<typename Fn> void numberAnonymousDeclsWithin(const DeclContext *DC,
92                                                       Fn Visit) {
93   unsigned Index = 0;
94   for (Decl *LexicalD : DC->decls()) {
95     // For a friend decl, we care about the declaration within it, if any.
96     if (auto *FD = dyn_cast<FriendDecl>(LexicalD))
97       LexicalD = FD->getFriendDecl();
98 
99     auto *ND = dyn_cast_or_null<NamedDecl>(LexicalD);
100     if (!ND || !needsAnonymousDeclarationNumber(ND))
101       continue;
102 
103     Visit(ND, Index++);
104   }
105 }
106 
107 } // namespace serialization
108 
109 } // namespace clang
110 
111 #endif
112