clang  14.0.0git
ASTCommon.h
Go to the documentation of this file.
1 //===- ASTCommon.h - Common stuff for ASTReader/ASTWriter -*- C++ -*-=========//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines common functions that both ASTReader and ASTWriter use.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
14 #define LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
15 
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/DeclFriend.h"
19 
20 namespace clang {
21 
22 namespace serialization {
23 
45 };
46 
47 TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT);
48 
49 template <typename IdxForTypeTy>
50 TypeID MakeTypeID(ASTContext &Context, QualType T, IdxForTypeTy IdxForType) {
51  if (T.isNull())
52  return PREDEF_TYPE_NULL_ID;
53 
54  unsigned FastQuals = T.getLocalFastQualifiers();
56 
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 
65  if (T == Context.AutoDeductTy)
66  return TypeIdx(PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
67  if (T == Context.AutoRRefDeductTy)
69 
70  return IdxForType(T).asTypeID(FastQuals);
71 }
72 
73 unsigned ComputeHash(Selector Sel);
74 
75 /// Retrieve the "definitive" declaration that provides all of the
76 /// 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.
86 
87 /// Determine whether the given declaration kind is redeclarable.
88 bool isRedeclarableDeclKind(unsigned Kind);
89 
90 /// Determine whether the given declaration needs an anonymous
91 /// declaration number.
93 
94 /// Visit each declaration within \c DC that needs an anonymous
95 /// declaration number and call \p Visit with the declaration and its number.
96 template<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 
112 /// 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.
116 inline 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 
127 } // namespace serialization
128 
129 } // namespace clang
130 
131 #endif
clang::BuiltinType
This class is used for builtin types like 'int'.
Definition: Type.h:2493
clang::serialization::TypeIdx
A type index; the type ID with the qualifier bits removed.
Definition: ASTBitCodes.h:88
clang::DeclContext::decls
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition: DeclBase.h:2115
clang::DeclContext
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1347
clang::serialization::PREDEF_TYPE_AUTO_DEDUCT
@ PREDEF_TYPE_AUTO_DEDUCT
The "auto" deduction type.
Definition: ASTBitCodes.h:939
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:249
clang::QualType::hasLocalNonFastQualifiers
bool hasLocalNonFastQualifiers() const
Determine whether this particular QualType instance has any "non-fast" qualifiers,...
Definition: Type.h:785
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
clang::QualType::getLocalFastQualifiers
unsigned getLocalFastQualifiers() const
Definition: Type.h:700
clang::serialization::UPD_CXX_POINT_OF_INSTANTIATION
@ UPD_CXX_POINT_OF_INSTANTIATION
Definition: ASTCommon.h:30
clang::serialization::UPD_CXX_RESOLVED_EXCEPTION_SPEC
@ UPD_CXX_RESOLVED_EXCEPTION_SPEC
Definition: ASTCommon.h:35
clang::serialization::UPD_DECL_MARKED_OPENMP_DECLARETARGET
@ UPD_DECL_MARKED_OPENMP_DECLARETARGET
Definition: ASTCommon.h:42
clang::serialization::UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT
@ UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT
Definition: ASTCommon.h:32
clang::serialization::UPD_STATIC_LOCAL_NUMBER
@ UPD_STATIC_LOCAL_NUMBER
Definition: ASTCommon.h:39
clang::serialization::isPartOfPerModuleInitializer
bool isPartOfPerModuleInitializer(const Decl *D)
Determine whether the given declaration will be included in the per-module initializer if it needs to...
Definition: ASTCommon.h:116
clang::serialization::UPD_CXX_ADDED_VAR_DEFINITION
@ UPD_CXX_ADDED_VAR_DEFINITION
Definition: ASTCommon.h:29
clang::serialization::DeclUpdateKind
DeclUpdateKind
Definition: ASTCommon.h:24
clang::serialization::PREDEF_TYPE_NULL_ID
@ PREDEF_TYPE_NULL_ID
The NULL type.
Definition: ASTBitCodes.h:846
clang::serialization::needsAnonymousDeclarationNumber
bool needsAnonymousDeclarationNumber(const NamedDecl *D)
Determine whether the given declaration needs an anonymous declaration number.
Definition: ASTCommon.cpp:448
DeclFriend.h
clang::serialization::UPD_DECL_EXPORTED
@ UPD_DECL_EXPORTED
Definition: ASTCommon.h:43
clang::serialization::UPD_ADDED_ATTR_TO_RECORD
@ UPD_ADDED_ATTR_TO_RECORD
Definition: ASTCommon.h:44
clang::serialization::getDefinitiveDeclContext
const DeclContext * getDefinitiveDeclContext(const DeclContext *DC)
Retrieve the "definitive" declaration that provides all of the visible entries for the given declarat...
Definition: ASTCommon.cpp:288
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:211
clang::serialization::UPD_DECL_MARKED_OPENMP_THREADPRIVATE
@ UPD_DECL_MARKED_OPENMP_THREADPRIVATE
Definition: ASTCommon.h:40
ASTContext.h
clang::serialization::TypeID
uint32_t TypeID
An ID number that refers to a type in an AST file.
Definition: ASTBitCodes.h:85
clang::serialization::UPD_CXX_ADDED_FUNCTION_DEFINITION
@ UPD_CXX_ADDED_FUNCTION_DEFINITION
Definition: ASTCommon.h:28
clang::serialization::UPD_DECL_MARKED_USED
@ UPD_DECL_MARKED_USED
Definition: ASTCommon.h:37
clang::serialization::UPD_CXX_RESOLVED_DTOR_DELETE
@ UPD_CXX_RESOLVED_DTOR_DELETE
Definition: ASTCommon.h:34
clang::serialization::UPD_DECL_MARKED_OPENMP_ALLOCATE
@ UPD_DECL_MARKED_OPENMP_ALLOCATE
Definition: ASTCommon.h:41
clang::serialization::UPD_CXX_ADDED_IMPLICIT_MEMBER
@ UPD_CXX_ADDED_IMPLICIT_MEMBER
Definition: ASTCommon.h:25
clang::serialization::UPD_CXX_INSTANTIATED_CLASS_DEFINITION
@ UPD_CXX_INSTANTIATED_CLASS_DEFINITION
Definition: ASTCommon.h:31
ASTBitCodes.h
clang::serialization::UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION
@ UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION
Definition: ASTCommon.h:26
clang::serialization::numberAnonymousDeclsWithin
void numberAnonymousDeclsWithin(const DeclContext *DC, Fn Visit)
Visit each declaration within DC that needs an anonymous declaration number and call Visit with the d...
Definition: ASTCommon.h:96
clang::QualType::isNull
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:738
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::serialization::isRedeclarableDeclKind
bool isRedeclarableDeclKind(unsigned Kind)
Determine whether the given declaration kind is redeclarable.
Definition: ASTCommon.cpp:347
clang::ObjCPropertyAttribute::Kind
Kind
Definition: DeclObjCCommon.h:22
clang::serialization::PREDEF_TYPE_AUTO_RREF_DEDUCT
@ PREDEF_TYPE_AUTO_RREF_DEDUCT
The "auto &&" deduction type.
Definition: ASTBitCodes.h:942
clang::QualType::removeLocalFastQualifiers
void removeLocalFastQualifiers()
Definition: Type.h:880
clang
Definition: CalledOnceCheck.h:17
clang::Selector
Smart pointer class that efficiently represents Objective-C method names.
Definition: IdentifierTable.h:720
clang::serialization::TypeIdx::asTypeID
TypeID asTypeID(unsigned FastQuals) const
Definition: ASTBitCodes.h:97
clang::serialization::MakeTypeID
TypeID MakeTypeID(ASTContext &Context, QualType T, IdxForTypeTy IdxForType)
Definition: ASTCommon.h:50
clang::serialization::ComputeHash
unsigned ComputeHash(Selector Sel)
Definition: ASTCommon.cpp:276
clang::QualType::getTypePtr
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:6425
clang::isTemplateInstantiation
bool isTemplateInstantiation(TemplateSpecializationKind Kind)
Determine whether this template specialization kind refers to an instantiation of an entity (as oppos...
Definition: Specifiers.h:188
clang::serialization::UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER
@ UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER
Definition: ASTCommon.h:33
clang::serialization::TypeIdxFromBuiltin
TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT)
Definition: ASTCommon.cpp:26
clang::serialization::UPD_MANGLING_NUMBER
@ UPD_MANGLING_NUMBER
Definition: ASTCommon.h:38
clang::serialization::UPD_CXX_ADDED_ANONYMOUS_NAMESPACE
@ UPD_CXX_ADDED_ANONYMOUS_NAMESPACE
Definition: ASTCommon.h:27
clang::serialization::UPD_CXX_DEDUCED_RETURN_TYPE
@ UPD_CXX_DEDUCED_RETURN_TYPE
Definition: ASTCommon.h:36
clang::QualType::hasLocalQualifiers
bool hasLocalQualifiers() const
Determine whether this particular QualType instance has any qualifiers, without looking through any t...
Definition: Type.h:775