1 //===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines common functions that both ASTReader and ASTWriter use.
12 //===----------------------------------------------------------------------===//
14 #include "ASTCommon.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/DeclObjC.h"
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Serialization/ASTDeserializationListener.h"
19 #include "llvm/ADT/StringExtras.h"
21 using namespace clang;
23 // Give ASTDeserializationListener's VTable a home.
24 ASTDeserializationListener::~ASTDeserializationListener() { }
26 serialization::TypeIdx
27 serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
29 switch (BT->getKind()) {
30 case BuiltinType::Void: ID = PREDEF_TYPE_VOID_ID; break;
31 case BuiltinType::Bool: ID = PREDEF_TYPE_BOOL_ID; break;
32 case BuiltinType::Char_U: ID = PREDEF_TYPE_CHAR_U_ID; break;
33 case BuiltinType::UChar: ID = PREDEF_TYPE_UCHAR_ID; break;
34 case BuiltinType::UShort: ID = PREDEF_TYPE_USHORT_ID; break;
35 case BuiltinType::UInt: ID = PREDEF_TYPE_UINT_ID; break;
36 case BuiltinType::ULong: ID = PREDEF_TYPE_ULONG_ID; break;
37 case BuiltinType::ULongLong: ID = PREDEF_TYPE_ULONGLONG_ID; break;
38 case BuiltinType::UInt128: ID = PREDEF_TYPE_UINT128_ID; break;
39 case BuiltinType::Char_S: ID = PREDEF_TYPE_CHAR_S_ID; break;
40 case BuiltinType::SChar: ID = PREDEF_TYPE_SCHAR_ID; break;
41 case BuiltinType::WChar_S:
42 case BuiltinType::WChar_U: ID = PREDEF_TYPE_WCHAR_ID; break;
43 case BuiltinType::Short: ID = PREDEF_TYPE_SHORT_ID; break;
44 case BuiltinType::Int: ID = PREDEF_TYPE_INT_ID; break;
45 case BuiltinType::Long: ID = PREDEF_TYPE_LONG_ID; break;
46 case BuiltinType::LongLong: ID = PREDEF_TYPE_LONGLONG_ID; break;
47 case BuiltinType::Int128: ID = PREDEF_TYPE_INT128_ID; break;
48 case BuiltinType::Half: ID = PREDEF_TYPE_HALF_ID; break;
49 case BuiltinType::Float: ID = PREDEF_TYPE_FLOAT_ID; break;
50 case BuiltinType::Double: ID = PREDEF_TYPE_DOUBLE_ID; break;
51 case BuiltinType::LongDouble: ID = PREDEF_TYPE_LONGDOUBLE_ID; break;
52 case BuiltinType::NullPtr: ID = PREDEF_TYPE_NULLPTR_ID; break;
53 case BuiltinType::Char16: ID = PREDEF_TYPE_CHAR16_ID; break;
54 case BuiltinType::Char32: ID = PREDEF_TYPE_CHAR32_ID; break;
55 case BuiltinType::Overload: ID = PREDEF_TYPE_OVERLOAD_ID; break;
56 case BuiltinType::BoundMember:ID = PREDEF_TYPE_BOUND_MEMBER; break;
57 case BuiltinType::PseudoObject:ID = PREDEF_TYPE_PSEUDO_OBJECT;break;
58 case BuiltinType::Dependent: ID = PREDEF_TYPE_DEPENDENT_ID; break;
59 case BuiltinType::UnknownAny: ID = PREDEF_TYPE_UNKNOWN_ANY; break;
60 case BuiltinType::ARCUnbridgedCast:
61 ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST; break;
62 case BuiltinType::ObjCId: ID = PREDEF_TYPE_OBJC_ID; break;
63 case BuiltinType::ObjCClass: ID = PREDEF_TYPE_OBJC_CLASS; break;
64 case BuiltinType::ObjCSel: ID = PREDEF_TYPE_OBJC_SEL; break;
65 case BuiltinType::OCLImage1d: ID = PREDEF_TYPE_IMAGE1D_ID; break;
66 case BuiltinType::OCLImage1dArray: ID = PREDEF_TYPE_IMAGE1D_ARR_ID; break;
67 case BuiltinType::OCLImage1dBuffer: ID = PREDEF_TYPE_IMAGE1D_BUFF_ID; break;
68 case BuiltinType::OCLImage2d: ID = PREDEF_TYPE_IMAGE2D_ID; break;
69 case BuiltinType::OCLImage2dArray: ID = PREDEF_TYPE_IMAGE2D_ARR_ID; break;
70 case BuiltinType::OCLImage3d: ID = PREDEF_TYPE_IMAGE3D_ID; break;
71 case BuiltinType::OCLSampler: ID = PREDEF_TYPE_SAMPLER_ID; break;
72 case BuiltinType::OCLEvent: ID = PREDEF_TYPE_EVENT_ID; break;
73 case BuiltinType::BuiltinFn:
74 ID = PREDEF_TYPE_BUILTIN_FN; break;
81 unsigned serialization::ComputeHash(Selector Sel) {
82 unsigned N = Sel.getNumArgs();
86 for (unsigned I = 0; I != N; ++I)
87 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
88 R = llvm::HashString(II->getName(), R);
93 serialization::getDefinitiveDeclContext(const DeclContext *DC) {
94 switch (DC->getDeclKind()) {
95 // These entities may have multiple definitions.
96 case Decl::TranslationUnit:
97 case Decl::ExternCContext:
99 case Decl::LinkageSpec:
102 // C/C++ tag types can only be defined in one place.
105 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
109 // FIXME: These can be defined in one place... except special member
110 // functions and out-of-line definitions.
111 case Decl::CXXRecord:
112 case Decl::ClassTemplateSpecialization:
113 case Decl::ClassTemplatePartialSpecialization:
116 // Each function, method, and block declaration is its own DeclContext.
118 case Decl::CXXMethod:
119 case Decl::CXXConstructor:
120 case Decl::CXXDestructor:
121 case Decl::CXXConversion:
122 case Decl::ObjCMethod:
125 // Objective C categories, category implementations, and class
126 // implementations can only be defined in one place.
127 case Decl::ObjCCategory:
128 case Decl::ObjCCategoryImpl:
129 case Decl::ObjCImplementation:
132 case Decl::ObjCProtocol:
133 if (const ObjCProtocolDecl *Def
134 = cast<ObjCProtocolDecl>(DC)->getDefinition())
138 // FIXME: These are defined in one place, but properties in class extensions
139 // end up being back-patched into the main interface. See
140 // Sema::HandlePropertyInClassExtension for the offending code.
141 case Decl::ObjCInterface:
145 llvm_unreachable("Unhandled DeclContext in AST reader");
148 llvm_unreachable("Unhandled decl kind");
151 bool serialization::isRedeclarableDeclKind(unsigned Kind) {
152 switch (static_cast<Decl::Kind>(Kind)) {
153 case Decl::TranslationUnit:
154 case Decl::ExternCContext:
155 // Special case of a "merged" declaration.
158 case Decl::Namespace:
159 case Decl::NamespaceAlias:
161 case Decl::TypeAlias:
164 case Decl::CXXRecord:
165 case Decl::ClassTemplateSpecialization:
166 case Decl::ClassTemplatePartialSpecialization:
167 case Decl::VarTemplateSpecialization:
168 case Decl::VarTemplatePartialSpecialization:
170 case Decl::CXXMethod:
171 case Decl::CXXConstructor:
172 case Decl::CXXDestructor:
173 case Decl::CXXConversion:
174 case Decl::UsingShadow:
176 case Decl::FunctionTemplate:
177 case Decl::ClassTemplate:
178 case Decl::VarTemplate:
179 case Decl::TypeAliasTemplate:
180 case Decl::ObjCProtocol:
181 case Decl::ObjCInterface:
185 // Never redeclarable.
186 case Decl::UsingDirective:
188 case Decl::UnresolvedUsingTypename:
189 case Decl::TemplateTypeParm:
190 case Decl::EnumConstant:
191 case Decl::UnresolvedUsingValue:
192 case Decl::IndirectField:
194 case Decl::MSProperty:
196 case Decl::ObjCAtDefsField:
197 case Decl::NonTypeTemplateParm:
198 case Decl::TemplateTemplateParm:
200 case Decl::ObjCMethod:
201 case Decl::ObjCCategory:
202 case Decl::ObjCCategoryImpl:
203 case Decl::ObjCImplementation:
204 case Decl::ObjCProperty:
205 case Decl::ObjCCompatibleAlias:
206 case Decl::LinkageSpec:
207 case Decl::ObjCPropertyImpl:
208 case Decl::FileScopeAsm:
209 case Decl::AccessSpec:
211 case Decl::FriendTemplate:
212 case Decl::StaticAssert:
215 case Decl::ClassScopeFunctionSpecialization:
217 case Decl::OMPThreadPrivate:
220 // These indirectly derive from Redeclarable<T> but are not actually
222 case Decl::ImplicitParam:
224 case Decl::ObjCTypeParam:
228 llvm_unreachable("Unhandled declaration kind");
231 bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
232 // Friend declarations in dependent contexts aren't anonymous in the usual
233 // sense, but they cannot be found by name lookup in their semantic context
234 // (or indeed in any context), so we treat them as anonymous.
236 // This doesn't apply to friend tag decls; Sema makes those available to name
237 // lookup in the surrounding context.
238 if (D->getFriendObjectKind() &&
239 D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
240 // For function templates and class templates, the template is numbered and
242 if (auto *FD = dyn_cast<FunctionDecl>(D))
243 return !FD->getDescribedFunctionTemplate();
244 if (auto *RD = dyn_cast<CXXRecordDecl>(D))
245 return !RD->getDescribedClassTemplate();
249 // Otherwise, we only care about anonymous class members.
250 if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
252 return isa<TagDecl>(D) || isa<FieldDecl>(D);