blob: 79d1817159c6633f0e03cbcaff8762568dddba09 [file] [log] [blame]
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001//===- 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
Stephen Hines176edba2014-12-01 14:53:08 -080014#ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
15#define LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016
Douglas Gregor3b8043b2011-08-09 15:13:55 +000017#include "clang/AST/ASTContext.h"
Stephen Hines0e2c34f2015-03-23 12:09:02 -070018#include "clang/AST/DeclFriend.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Serialization/ASTBitCodes.h"
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +000020
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000021namespace clang {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000022
23namespace serialization {
24
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +000025enum DeclUpdateKind {
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +000026 UPD_CXX_ADDED_IMPLICIT_MEMBER,
Sebastian Redl7c0837f2011-04-24 16:28:13 +000027 UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
Sebastian Redlf79a7192011-04-29 08:19:30 +000028 UPD_CXX_ADDED_ANONYMOUS_NAMESPACE,
Stephen Hines176edba2014-12-01 14:53:08 -080029 UPD_CXX_ADDED_FUNCTION_DEFINITION,
Richard Smith9dadfab2013-05-11 05:45:24 +000030 UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
Stephen Hines6bcf27b2014-05-29 04:14:42 -070031 UPD_CXX_INSTANTIATED_CLASS_DEFINITION,
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070032 UPD_CXX_RESOLVED_DTOR_DELETE,
Stephen Hines651f13c2014-04-23 16:59:28 -070033 UPD_CXX_RESOLVED_EXCEPTION_SPEC,
Eli Friedman86164e82013-09-05 00:02:25 +000034 UPD_CXX_DEDUCED_RETURN_TYPE,
Stephen Hines651f13c2014-04-23 16:59:28 -070035 UPD_DECL_MARKED_USED,
36 UPD_MANGLING_NUMBER,
Stephen Hines176edba2014-12-01 14:53:08 -080037 UPD_STATIC_LOCAL_NUMBER,
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070038 UPD_DECL_MARKED_OPENMP_THREADPRIVATE,
39 UPD_DECL_EXPORTED
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +000040};
41
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +000042TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT);
43
44template <typename IdxForTypeTy>
Douglas Gregor3b8043b2011-08-09 15:13:55 +000045TypeID MakeTypeID(ASTContext &Context, QualType T, IdxForTypeTy IdxForType) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +000046 if (T.isNull())
47 return PREDEF_TYPE_NULL_ID;
48
49 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall49f4e1c2010-12-10 11:01:00 +000050 T.removeLocalFastQualifiers();
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +000051
52 if (T.hasLocalNonFastQualifiers())
53 return IdxForType(T).asTypeID(FastQuals);
54
55 assert(!T.hasLocalQualifiers());
56
57 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr()))
58 return TypeIdxFromBuiltin(BT).asTypeID(FastQuals);
59
Douglas Gregor3b8043b2011-08-09 15:13:55 +000060 if (T == Context.AutoDeductTy)
61 return TypeIdx(PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
62 if (T == Context.AutoRRefDeductTy)
63 return TypeIdx(PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
Meador Ingefb40e3f2012-07-01 15:57:25 +000064 if (T == Context.VaListTagTy)
65 return TypeIdx(PREDEF_TYPE_VA_LIST_TAG).asTypeID(FastQuals);
Douglas Gregor3b8043b2011-08-09 15:13:55 +000066
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +000067 return IdxForType(T).asTypeID(FastQuals);
68}
69
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000070unsigned ComputeHash(Selector Sel);
71
Douglas Gregor5a04f9f2013-01-21 15:25:38 +000072/// \brief Retrieve the "definitive" declaration that provides all of the
73/// visible entries for the given declaration context, if there is one.
74///
75/// The "definitive" declaration is the only place where we need to look to
76/// find information about the declarations within the given declaration
77/// context. For example, C++ and Objective-C classes, C structs/unions, and
78/// Objective-C protocols, categories, and extensions are all defined in a
79/// single place in the source code, so they have definitive declarations
80/// associated with them. C++ namespaces, on the other hand, can have
81/// multiple definitions.
Douglas Gregore0d20662013-01-22 17:08:30 +000082const DeclContext *getDefinitiveDeclContext(const DeclContext *DC);
Douglas Gregor5a04f9f2013-01-21 15:25:38 +000083
Douglas Gregor9cfdc032013-01-21 16:16:40 +000084/// \brief Determine whether the given declaration kind is redeclarable.
85bool isRedeclarableDeclKind(unsigned Kind);
86
Stephen Hines176edba2014-12-01 14:53:08 -080087/// \brief Determine whether the given declaration needs an anonymous
88/// declaration number.
89bool needsAnonymousDeclarationNumber(const NamedDecl *D);
90
Stephen Hines0e2c34f2015-03-23 12:09:02 -070091/// \brief Visit each declaration within \c DC that needs an anonymous
92/// declaration number and call \p Visit with the declaration and its number.
93template<typename Fn> void numberAnonymousDeclsWithin(const DeclContext *DC,
94 Fn Visit) {
95 unsigned Index = 0;
96 for (Decl *LexicalD : DC->decls()) {
97 // For a friend decl, we care about the declaration within it, if any.
98 if (auto *FD = dyn_cast<FriendDecl>(LexicalD))
99 LexicalD = FD->getFriendDecl();
100
101 auto *ND = dyn_cast_or_null<NamedDecl>(LexicalD);
102 if (!ND || !needsAnonymousDeclarationNumber(ND))
103 continue;
104
105 Visit(ND, Index++);
106 }
107}
108
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000109} // namespace serialization
110
111} // namespace clang
112
113#endif