blob: 296642e3674a49d4ac2855071e579ac24999f2ef [file] [log] [blame]
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001//===- ASTCommon.h - Common stuff for ASTReader/ASTWriter -*- C++ -*-=========//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines common functions that both ASTReader and ASTWriter use.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
14#define LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015
Douglas Gregoreda8e122011-08-09 15:13:55 +000016#include "clang/AST/ASTContext.h"
Richard Smith2b560572015-02-07 03:11:11 +000017#include "clang/AST/DeclFriend.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Serialization/ASTBitCodes.h"
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +000019
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000020namespace clang {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000021
22namespace serialization {
23
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +000024enum DeclUpdateKind {
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +000025 UPD_CXX_ADDED_IMPLICIT_MEMBER,
Sebastian Redlfa1f3702011-04-24 16:28:13 +000026 UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
Sebastian Redl2ac2c722011-04-29 08:19:30 +000027 UPD_CXX_ADDED_ANONYMOUS_NAMESPACE,
Richard Smith4d235792014-08-07 18:53:08 +000028 UPD_CXX_ADDED_FUNCTION_DEFINITION,
Richard Smith891fc7f2017-12-05 01:31:47 +000029 UPD_CXX_ADDED_VAR_DEFINITION,
30 UPD_CXX_POINT_OF_INSTANTIATION,
Richard Smithcd45dbc2014-04-19 03:48:30 +000031 UPD_CXX_INSTANTIATED_CLASS_DEFINITION,
John McCall32791cc2016-01-06 22:34:54 +000032 UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT,
Richard Smith4b054b22016-08-24 21:25:37 +000033 UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER,
Richard Smithf8134002015-03-10 01:41:22 +000034 UPD_CXX_RESOLVED_DTOR_DELETE,
Richard Smith564417a2014-03-20 21:47:22 +000035 UPD_CXX_RESOLVED_EXCEPTION_SPEC,
Eli Friedman276dd182013-09-05 00:02:25 +000036 UPD_CXX_DEDUCED_RETURN_TYPE,
Richard Smith5652c0f2014-03-21 01:48:23 +000037 UPD_DECL_MARKED_USED,
38 UPD_MANGLING_NUMBER,
Alexey Bataev97720002014-11-11 04:05:39 +000039 UPD_STATIC_LOCAL_NUMBER,
Richard Smith65ebb4a2015-03-26 04:09:53 +000040 UPD_DECL_MARKED_OPENMP_THREADPRIVATE,
Alexey Bataev25ed0c02019-03-07 17:54:44 +000041 UPD_DECL_MARKED_OPENMP_ALLOCATE,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000042 UPD_DECL_MARKED_OPENMP_DECLARETARGET,
Alex Denisovfde64952015-06-26 05:28:36 +000043 UPD_DECL_EXPORTED,
44 UPD_ADDED_ATTR_TO_RECORD
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +000045};
46
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +000047TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT);
48
49template <typename IdxForTypeTy>
Douglas Gregoreda8e122011-08-09 15:13:55 +000050TypeID MakeTypeID(ASTContext &Context, QualType T, IdxForTypeTy IdxForType) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +000051 if (T.isNull())
52 return PREDEF_TYPE_NULL_ID;
53
54 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall717d9b02010-12-10 11:01:00 +000055 T.removeLocalFastQualifiers();
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +000056
57 if (T.hasLocalNonFastQualifiers())
58 return IdxForType(T).asTypeID(FastQuals);
59
60 assert(!T.hasLocalQualifiers());
61
62 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr()))
63 return TypeIdxFromBuiltin(BT).asTypeID(FastQuals);
64
Douglas Gregoreda8e122011-08-09 15:13:55 +000065 if (T == Context.AutoDeductTy)
66 return TypeIdx(PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
67 if (T == Context.AutoRRefDeductTy)
68 return TypeIdx(PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
69
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +000070 return IdxForType(T).asTypeID(FastQuals);
71}
72
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000073unsigned ComputeHash(Selector Sel);
74
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000075/// Retrieve the "definitive" declaration that provides all of the
Douglas Gregor9f782892013-01-21 15:25:38 +000076/// visible entries for the given declaration context, if there is one.
77///
78/// The "definitive" declaration is the only place where we need to look to
79/// find information about the declarations within the given declaration
80/// context. For example, C++ and Objective-C classes, C structs/unions, and
81/// Objective-C protocols, categories, and extensions are all defined in a
82/// single place in the source code, so they have definitive declarations
83/// associated with them. C++ namespaces, on the other hand, can have
84/// multiple definitions.
Douglas Gregor7a6e2002013-01-22 17:08:30 +000085const DeclContext *getDefinitiveDeclContext(const DeclContext *DC);
Douglas Gregor9f782892013-01-21 15:25:38 +000086
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000087/// Determine whether the given declaration kind is redeclarable.
Douglas Gregorfe732d52013-01-21 16:16:40 +000088bool isRedeclarableDeclKind(unsigned Kind);
89
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000090/// Determine whether the given declaration needs an anonymous
Richard Smithd08aeb62014-08-28 01:33:39 +000091/// declaration number.
92bool needsAnonymousDeclarationNumber(const NamedDecl *D);
93
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000094/// Visit each declaration within \c DC that needs an anonymous
Richard Smith2b560572015-02-07 03:11:11 +000095/// declaration number and call \p Visit with the declaration and its number.
96template<typename Fn> void numberAnonymousDeclsWithin(const DeclContext *DC,
97 Fn Visit) {
98 unsigned Index = 0;
99 for (Decl *LexicalD : DC->decls()) {
100 // For a friend decl, we care about the declaration within it, if any.
101 if (auto *FD = dyn_cast<FriendDecl>(LexicalD))
102 LexicalD = FD->getFriendDecl();
103
104 auto *ND = dyn_cast_or_null<NamedDecl>(LexicalD);
105 if (!ND || !needsAnonymousDeclarationNumber(ND))
106 continue;
107
108 Visit(ND, Index++);
109 }
110}
111
Richard Smith520a37f2019-02-05 23:37:13 +0000112/// Determine whether the given declaration will be included in the per-module
113/// initializer if it needs to be eagerly handed to the AST consumer. If so, we
114/// should not hand it to the consumer when deserializing it, nor include it in
115/// the list of eagerly deserialized declarations.
116inline bool isPartOfPerModuleInitializer(const Decl *D) {
117 if (isa<ImportDecl>(D))
118 return true;
119 // Template instantiations are notionally in an "instantiation unit" rather
120 // than in any particular translation unit, so they need not be part of any
121 // particular (sub)module's per-module initializer.
122 if (auto *VD = dyn_cast<VarDecl>(D))
123 return !isTemplateInstantiation(VD->getTemplateSpecializationKind());
124 return false;
125}
126
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000127} // namespace serialization
128
129} // namespace clang
130
131#endif