blob: f21e8a7ea030bcf19dfe180569d4867302395de7 [file] [log] [blame]
Argyrios Kyrtzidis4bd97102010-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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
15#define LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016
Douglas Gregoreda8e122011-08-09 15:13:55 +000017#include "clang/AST/ASTContext.h"
Richard Smith2b560572015-02-07 03:11:11 +000018#include "clang/AST/DeclFriend.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Serialization/ASTBitCodes.h"
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +000020
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000021namespace clang {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000022
23namespace serialization {
24
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +000025enum DeclUpdateKind {
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +000026 UPD_CXX_ADDED_IMPLICIT_MEMBER,
Sebastian Redlfa1f3702011-04-24 16:28:13 +000027 UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
Sebastian Redl2ac2c722011-04-29 08:19:30 +000028 UPD_CXX_ADDED_ANONYMOUS_NAMESPACE,
Richard Smith4d235792014-08-07 18:53:08 +000029 UPD_CXX_ADDED_FUNCTION_DEFINITION,
Richard Smith1fa5d642013-05-11 05:45:24 +000030 UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
Richard Smithcd45dbc2014-04-19 03:48:30 +000031 UPD_CXX_INSTANTIATED_CLASS_DEFINITION,
Richard Smithf8134002015-03-10 01:41:22 +000032 UPD_CXX_RESOLVED_DTOR_DELETE,
Richard Smith564417a2014-03-20 21:47:22 +000033 UPD_CXX_RESOLVED_EXCEPTION_SPEC,
Eli Friedman276dd182013-09-05 00:02:25 +000034 UPD_CXX_DEDUCED_RETURN_TYPE,
Richard Smith5652c0f2014-03-21 01:48:23 +000035 UPD_DECL_MARKED_USED,
36 UPD_MANGLING_NUMBER,
Alexey Bataev97720002014-11-11 04:05:39 +000037 UPD_STATIC_LOCAL_NUMBER,
Richard Smith65ebb4a2015-03-26 04:09:53 +000038 UPD_DECL_MARKED_OPENMP_THREADPRIVATE,
Alex Denisovfde64952015-06-26 05:28:36 +000039 UPD_DECL_EXPORTED,
40 UPD_ADDED_ATTR_TO_RECORD
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +000041};
42
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +000043TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT);
44
45template <typename IdxForTypeTy>
Douglas Gregoreda8e122011-08-09 15:13:55 +000046TypeID MakeTypeID(ASTContext &Context, QualType T, IdxForTypeTy IdxForType) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +000047 if (T.isNull())
48 return PREDEF_TYPE_NULL_ID;
49
50 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall717d9b02010-12-10 11:01:00 +000051 T.removeLocalFastQualifiers();
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +000052
53 if (T.hasLocalNonFastQualifiers())
54 return IdxForType(T).asTypeID(FastQuals);
55
56 assert(!T.hasLocalQualifiers());
57
58 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr()))
59 return TypeIdxFromBuiltin(BT).asTypeID(FastQuals);
60
Douglas Gregoreda8e122011-08-09 15:13:55 +000061 if (T == Context.AutoDeductTy)
62 return TypeIdx(PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
63 if (T == Context.AutoRRefDeductTy)
64 return TypeIdx(PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
Meador Ingecfb60902012-07-01 15:57:25 +000065 if (T == Context.VaListTagTy)
66 return TypeIdx(PREDEF_TYPE_VA_LIST_TAG).asTypeID(FastQuals);
Douglas Gregoreda8e122011-08-09 15:13:55 +000067
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +000068 return IdxForType(T).asTypeID(FastQuals);
69}
70
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000071unsigned ComputeHash(Selector Sel);
72
Douglas Gregor9f782892013-01-21 15:25:38 +000073/// \brief Retrieve the "definitive" declaration that provides all of the
74/// visible entries for the given declaration context, if there is one.
75///
76/// The "definitive" declaration is the only place where we need to look to
77/// find information about the declarations within the given declaration
78/// context. For example, C++ and Objective-C classes, C structs/unions, and
79/// Objective-C protocols, categories, and extensions are all defined in a
80/// single place in the source code, so they have definitive declarations
81/// associated with them. C++ namespaces, on the other hand, can have
82/// multiple definitions.
Douglas Gregor7a6e2002013-01-22 17:08:30 +000083const DeclContext *getDefinitiveDeclContext(const DeclContext *DC);
Douglas Gregor9f782892013-01-21 15:25:38 +000084
Douglas Gregorfe732d52013-01-21 16:16:40 +000085/// \brief Determine whether the given declaration kind is redeclarable.
86bool isRedeclarableDeclKind(unsigned Kind);
87
Richard Smithd08aeb62014-08-28 01:33:39 +000088/// \brief Determine whether the given declaration needs an anonymous
89/// declaration number.
90bool needsAnonymousDeclarationNumber(const NamedDecl *D);
91
Richard Smith2b560572015-02-07 03:11:11 +000092/// \brief Visit each declaration within \c DC that needs an anonymous
93/// declaration number and call \p Visit with the declaration and its number.
94template<typename Fn> void numberAnonymousDeclsWithin(const DeclContext *DC,
95 Fn Visit) {
96 unsigned Index = 0;
97 for (Decl *LexicalD : DC->decls()) {
98 // For a friend decl, we care about the declaration within it, if any.
99 if (auto *FD = dyn_cast<FriendDecl>(LexicalD))
100 LexicalD = FD->getFriendDecl();
101
102 auto *ND = dyn_cast_or_null<NamedDecl>(LexicalD);
103 if (!ND || !needsAnonymousDeclarationNumber(ND))
104 continue;
105
106 Visit(ND, Index++);
107 }
108}
109
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000110} // namespace serialization
111
112} // namespace clang
113
114#endif