blob: 85c574c2021ab1c40f462eade7be44d9e9c435ad [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- ASTCommon.cpp - 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
14#include "ASTCommon.h"
Richard Smithd08aeb62014-08-28 01:33:39 +000015#include "clang/AST/DeclCXX.h"
Douglas Gregor9f782892013-01-21 15:25:38 +000016#include "clang/AST/DeclObjC.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "clang/Basic/IdentifierTable.h"
18#include "clang/Serialization/ASTDeserializationListener.h"
19#include "llvm/ADT/StringExtras.h"
20
21using namespace clang;
22
23// Give ASTDeserializationListener's VTable a home.
24ASTDeserializationListener::~ASTDeserializationListener() { }
25
26serialization::TypeIdx
27serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
28 unsigned ID = 0;
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;
Guy Benyeid8a08ea2012-12-18 14:38:23 +000065 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;
Guy Benyei61054192013-02-07 10:55:47 +000071 case BuiltinType::OCLSampler: ID = PREDEF_TYPE_SAMPLER_ID; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +000072 case BuiltinType::OCLEvent: ID = PREDEF_TYPE_EVENT_ID; break;
Guy Benyei11169dd2012-12-18 14:30:41 +000073 case BuiltinType::BuiltinFn:
74 ID = PREDEF_TYPE_BUILTIN_FN; break;
75
76 }
77
78 return TypeIdx(ID);
79}
80
81unsigned serialization::ComputeHash(Selector Sel) {
82 unsigned N = Sel.getNumArgs();
83 if (N == 0)
84 ++N;
85 unsigned R = 5381;
86 for (unsigned I = 0; I != N; ++I)
87 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
88 R = llvm::HashString(II->getName(), R);
89 return R;
90}
Douglas Gregor9f782892013-01-21 15:25:38 +000091
Douglas Gregor7a6e2002013-01-22 17:08:30 +000092const DeclContext *
93serialization::getDefinitiveDeclContext(const DeclContext *DC) {
Douglas Gregor9f782892013-01-21 15:25:38 +000094 switch (DC->getDeclKind()) {
95 // These entities may have multiple definitions.
96 case Decl::TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +000097 case Decl::ExternCContext:
Douglas Gregor9f782892013-01-21 15:25:38 +000098 case Decl::Namespace:
99 case Decl::LinkageSpec:
Craig Toppera13603a2014-05-22 05:54:18 +0000100 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000101
102 // C/C++ tag types can only be defined in one place.
103 case Decl::Enum:
104 case Decl::Record:
105 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
106 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000107 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000108
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:
Craig Toppera13603a2014-05-22 05:54:18 +0000114 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000115
116 // Each function, method, and block declaration is its own DeclContext.
117 case Decl::Function:
118 case Decl::CXXMethod:
119 case Decl::CXXConstructor:
120 case Decl::CXXDestructor:
121 case Decl::CXXConversion:
122 case Decl::ObjCMethod:
123 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000124 case Decl::Captured:
Douglas Gregor9f782892013-01-21 15:25:38 +0000125 // 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:
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000130 return DC;
Douglas Gregor9f782892013-01-21 15:25:38 +0000131
132 case Decl::ObjCProtocol:
133 if (const ObjCProtocolDecl *Def
134 = cast<ObjCProtocolDecl>(DC)->getDefinition())
135 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000136 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000137
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:
Craig Toppera13603a2014-05-22 05:54:18 +0000142 return nullptr;
143
Douglas Gregor9f782892013-01-21 15:25:38 +0000144 default:
145 llvm_unreachable("Unhandled DeclContext in AST reader");
146 }
147
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000148 llvm_unreachable("Unhandled decl kind");
Douglas Gregorfe732d52013-01-21 16:16:40 +0000149}
Douglas Gregor9f782892013-01-21 15:25:38 +0000150
Douglas Gregorfe732d52013-01-21 16:16:40 +0000151bool serialization::isRedeclarableDeclKind(unsigned Kind) {
152 switch (static_cast<Decl::Kind>(Kind)) {
Richard Smithf19e1272015-03-07 00:04:49 +0000153 case Decl::TranslationUnit:
154 case Decl::ExternCContext:
155 // Special case of a "merged" declaration.
156 return true;
157
Douglas Gregorfe732d52013-01-21 16:16:40 +0000158 case Decl::Namespace:
Richard Smithf4634362014-09-03 23:11:22 +0000159 case Decl::NamespaceAlias:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000160 case Decl::Typedef:
161 case Decl::TypeAlias:
162 case Decl::Enum:
163 case Decl::Record:
164 case Decl::CXXRecord:
165 case Decl::ClassTemplateSpecialization:
166 case Decl::ClassTemplatePartialSpecialization:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000167 case Decl::VarTemplateSpecialization:
168 case Decl::VarTemplatePartialSpecialization:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000169 case Decl::Function:
170 case Decl::CXXMethod:
171 case Decl::CXXConstructor:
172 case Decl::CXXDestructor:
173 case Decl::CXXConversion:
Richard Smithfd8634a2013-10-23 02:17:46 +0000174 case Decl::UsingShadow:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000175 case Decl::Var:
176 case Decl::FunctionTemplate:
177 case Decl::ClassTemplate:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000178 case Decl::VarTemplate:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000179 case Decl::TypeAliasTemplate:
180 case Decl::ObjCProtocol:
181 case Decl::ObjCInterface:
Michael Han84324352013-02-22 17:15:32 +0000182 case Decl::Empty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000183 return true;
184
185 // Never redeclarable.
186 case Decl::UsingDirective:
187 case Decl::Label:
188 case Decl::UnresolvedUsingTypename:
189 case Decl::TemplateTypeParm:
190 case Decl::EnumConstant:
191 case Decl::UnresolvedUsingValue:
192 case Decl::IndirectField:
193 case Decl::Field:
John McCall5e77d762013-04-16 07:28:30 +0000194 case Decl::MSProperty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000195 case Decl::ObjCIvar:
196 case Decl::ObjCAtDefsField:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000197 case Decl::NonTypeTemplateParm:
198 case Decl::TemplateTemplateParm:
199 case Decl::Using:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000200 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:
210 case Decl::Friend:
211 case Decl::FriendTemplate:
212 case Decl::StaticAssert:
213 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000214 case Decl::Captured:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000215 case Decl::ClassScopeFunctionSpecialization:
216 case Decl::Import:
Alexey Bataeva769e072013-03-22 06:34:35 +0000217 case Decl::OMPThreadPrivate:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000218 return false;
Richard Smithf4634362014-09-03 23:11:22 +0000219
220 // These indirectly derive from Redeclarable<T> but are not actually
221 // redeclarable.
222 case Decl::ImplicitParam:
223 case Decl::ParmVar:
224 return false;
Douglas Gregorfe732d52013-01-21 16:16:40 +0000225 }
226
227 llvm_unreachable("Unhandled declaration kind");
Douglas Gregor9f782892013-01-21 15:25:38 +0000228}
Richard Smithd08aeb62014-08-28 01:33:39 +0000229
230bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
Richard Smith2b560572015-02-07 03:11:11 +0000231 // Friend declarations in dependent contexts aren't anonymous in the usual
232 // sense, but they cannot be found by name lookup in their semantic context
233 // (or indeed in any context), so we treat them as anonymous.
234 //
235 // This doesn't apply to friend tag decls; Sema makes those available to name
236 // lookup in the surrounding context.
237 if (D->getFriendObjectKind() &&
238 D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
239 // For function templates and class templates, the template is numbered and
240 // not its pattern.
241 if (auto *FD = dyn_cast<FunctionDecl>(D))
242 return !FD->getDescribedFunctionTemplate();
243 if (auto *RD = dyn_cast<CXXRecordDecl>(D))
244 return !RD->getDescribedClassTemplate();
245 return true;
246 }
247
248 // Otherwise, we only care about anonymous class members.
Richard Smithd08aeb62014-08-28 01:33:39 +0000249 if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
250 return false;
251 return isa<TagDecl>(D) || isa<FieldDecl>(D);
252}
253