blob: ec1a936684f425f4db0e5c9f36fc0776f90d53ae [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.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000024ASTDeserializationListener::~ASTDeserializationListener() { }
Guy Benyei11169dd2012-12-18 14:30:41 +000025
26serialization::TypeIdx
27serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
28 unsigned ID = 0;
29 switch (BT->getKind()) {
Alexey Baderbdf7c842015-09-15 12:18:29 +000030 case BuiltinType::Void:
31 ID = PREDEF_TYPE_VOID_ID;
32 break;
33 case BuiltinType::Bool:
34 ID = PREDEF_TYPE_BOOL_ID;
35 break;
36 case BuiltinType::Char_U:
37 ID = PREDEF_TYPE_CHAR_U_ID;
38 break;
39 case BuiltinType::UChar:
40 ID = PREDEF_TYPE_UCHAR_ID;
41 break;
42 case BuiltinType::UShort:
43 ID = PREDEF_TYPE_USHORT_ID;
44 break;
45 case BuiltinType::UInt:
46 ID = PREDEF_TYPE_UINT_ID;
47 break;
48 case BuiltinType::ULong:
49 ID = PREDEF_TYPE_ULONG_ID;
50 break;
51 case BuiltinType::ULongLong:
52 ID = PREDEF_TYPE_ULONGLONG_ID;
53 break;
54 case BuiltinType::UInt128:
55 ID = PREDEF_TYPE_UINT128_ID;
56 break;
57 case BuiltinType::Char_S:
58 ID = PREDEF_TYPE_CHAR_S_ID;
59 break;
60 case BuiltinType::SChar:
61 ID = PREDEF_TYPE_SCHAR_ID;
62 break;
Guy Benyei11169dd2012-12-18 14:30:41 +000063 case BuiltinType::WChar_S:
Alexey Baderbdf7c842015-09-15 12:18:29 +000064 case BuiltinType::WChar_U:
65 ID = PREDEF_TYPE_WCHAR_ID;
66 break;
67 case BuiltinType::Short:
68 ID = PREDEF_TYPE_SHORT_ID;
69 break;
70 case BuiltinType::Int:
71 ID = PREDEF_TYPE_INT_ID;
72 break;
73 case BuiltinType::Long:
74 ID = PREDEF_TYPE_LONG_ID;
75 break;
76 case BuiltinType::LongLong:
77 ID = PREDEF_TYPE_LONGLONG_ID;
78 break;
79 case BuiltinType::Int128:
80 ID = PREDEF_TYPE_INT128_ID;
81 break;
82 case BuiltinType::Half:
83 ID = PREDEF_TYPE_HALF_ID;
84 break;
85 case BuiltinType::Float:
86 ID = PREDEF_TYPE_FLOAT_ID;
87 break;
88 case BuiltinType::Double:
89 ID = PREDEF_TYPE_DOUBLE_ID;
90 break;
91 case BuiltinType::LongDouble:
92 ID = PREDEF_TYPE_LONGDOUBLE_ID;
93 break;
94 case BuiltinType::NullPtr:
95 ID = PREDEF_TYPE_NULLPTR_ID;
96 break;
97 case BuiltinType::Char16:
98 ID = PREDEF_TYPE_CHAR16_ID;
99 break;
100 case BuiltinType::Char32:
101 ID = PREDEF_TYPE_CHAR32_ID;
102 break;
103 case BuiltinType::Overload:
104 ID = PREDEF_TYPE_OVERLOAD_ID;
105 break;
106 case BuiltinType::BoundMember:
107 ID = PREDEF_TYPE_BOUND_MEMBER;
108 break;
109 case BuiltinType::PseudoObject:
110 ID = PREDEF_TYPE_PSEUDO_OBJECT;
111 break;
112 case BuiltinType::Dependent:
113 ID = PREDEF_TYPE_DEPENDENT_ID;
114 break;
115 case BuiltinType::UnknownAny:
116 ID = PREDEF_TYPE_UNKNOWN_ANY;
117 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000118 case BuiltinType::ARCUnbridgedCast:
Alexey Baderbdf7c842015-09-15 12:18:29 +0000119 ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;
120 break;
121 case BuiltinType::ObjCId:
122 ID = PREDEF_TYPE_OBJC_ID;
123 break;
124 case BuiltinType::ObjCClass:
125 ID = PREDEF_TYPE_OBJC_CLASS;
126 break;
127 case BuiltinType::ObjCSel:
128 ID = PREDEF_TYPE_OBJC_SEL;
129 break;
130 case BuiltinType::OCLImage1d:
131 ID = PREDEF_TYPE_IMAGE1D_ID;
132 break;
133 case BuiltinType::OCLImage1dArray:
134 ID = PREDEF_TYPE_IMAGE1D_ARR_ID;
135 break;
136 case BuiltinType::OCLImage1dBuffer:
137 ID = PREDEF_TYPE_IMAGE1D_BUFF_ID;
138 break;
139 case BuiltinType::OCLImage2d:
140 ID = PREDEF_TYPE_IMAGE2D_ID;
141 break;
142 case BuiltinType::OCLImage2dArray:
143 ID = PREDEF_TYPE_IMAGE2D_ARR_ID;
144 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +0000145 case BuiltinType::OCLImage2dDepth:
146 ID = PREDEF_TYPE_IMAGE2D_DEP_ID;
147 break;
148 case BuiltinType::OCLImage2dArrayDepth:
149 ID = PREDEF_TYPE_IMAGE2D_ARR_DEP_ID;
150 break;
151 case BuiltinType::OCLImage2dMSAA:
152 ID = PREDEF_TYPE_IMAGE2D_MSAA_ID;
153 break;
154 case BuiltinType::OCLImage2dArrayMSAA:
155 ID = PREDEF_TYPE_IMAGE2D_ARR_MSAA_ID;
156 break;
157 case BuiltinType::OCLImage2dMSAADepth:
158 ID = PREDEF_TYPE_IMAGE2D_MSAA_DEP_ID;
159 break;
160 case BuiltinType::OCLImage2dArrayMSAADepth:
161 ID = PREDEF_TYPE_IMAGE2D_ARR_MSAA_DEPTH_ID;
162 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +0000163 case BuiltinType::OCLImage3d:
164 ID = PREDEF_TYPE_IMAGE3D_ID;
165 break;
166 case BuiltinType::OCLSampler:
167 ID = PREDEF_TYPE_SAMPLER_ID;
168 break;
169 case BuiltinType::OCLEvent:
170 ID = PREDEF_TYPE_EVENT_ID;
171 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +0000172 case BuiltinType::OCLClkEvent:
173 ID = PREDEF_TYPE_CLK_EVENT_ID;
174 break;
175 case BuiltinType::OCLQueue:
176 ID = PREDEF_TYPE_QUEUE_ID;
177 break;
178 case BuiltinType::OCLNDRange:
179 ID = PREDEF_TYPE_NDRANGE_ID;
180 break;
181 case BuiltinType::OCLReserveID:
182 ID = PREDEF_TYPE_RESERVE_ID_ID;
183 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000184 case BuiltinType::BuiltinFn:
Alexey Baderbdf7c842015-09-15 12:18:29 +0000185 ID = PREDEF_TYPE_BUILTIN_FN;
186 break;
187 case BuiltinType::OMPArraySection:
188 ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
189 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000190 }
191
192 return TypeIdx(ID);
193}
194
195unsigned serialization::ComputeHash(Selector Sel) {
196 unsigned N = Sel.getNumArgs();
197 if (N == 0)
198 ++N;
199 unsigned R = 5381;
200 for (unsigned I = 0; I != N; ++I)
201 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
202 R = llvm::HashString(II->getName(), R);
203 return R;
204}
Douglas Gregor9f782892013-01-21 15:25:38 +0000205
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000206const DeclContext *
207serialization::getDefinitiveDeclContext(const DeclContext *DC) {
Douglas Gregor9f782892013-01-21 15:25:38 +0000208 switch (DC->getDeclKind()) {
209 // These entities may have multiple definitions.
210 case Decl::TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +0000211 case Decl::ExternCContext:
Douglas Gregor9f782892013-01-21 15:25:38 +0000212 case Decl::Namespace:
213 case Decl::LinkageSpec:
Craig Toppera13603a2014-05-22 05:54:18 +0000214 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000215
216 // C/C++ tag types can only be defined in one place.
217 case Decl::Enum:
218 case Decl::Record:
219 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
220 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000221 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000222
223 // FIXME: These can be defined in one place... except special member
224 // functions and out-of-line definitions.
225 case Decl::CXXRecord:
226 case Decl::ClassTemplateSpecialization:
227 case Decl::ClassTemplatePartialSpecialization:
Craig Toppera13603a2014-05-22 05:54:18 +0000228 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000229
230 // Each function, method, and block declaration is its own DeclContext.
231 case Decl::Function:
232 case Decl::CXXMethod:
233 case Decl::CXXConstructor:
234 case Decl::CXXDestructor:
235 case Decl::CXXConversion:
236 case Decl::ObjCMethod:
237 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000238 case Decl::Captured:
Douglas Gregor9f782892013-01-21 15:25:38 +0000239 // Objective C categories, category implementations, and class
240 // implementations can only be defined in one place.
241 case Decl::ObjCCategory:
242 case Decl::ObjCCategoryImpl:
243 case Decl::ObjCImplementation:
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000244 return DC;
Douglas Gregor9f782892013-01-21 15:25:38 +0000245
246 case Decl::ObjCProtocol:
247 if (const ObjCProtocolDecl *Def
248 = cast<ObjCProtocolDecl>(DC)->getDefinition())
249 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000250 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000251
252 // FIXME: These are defined in one place, but properties in class extensions
253 // end up being back-patched into the main interface. See
254 // Sema::HandlePropertyInClassExtension for the offending code.
255 case Decl::ObjCInterface:
Craig Toppera13603a2014-05-22 05:54:18 +0000256 return nullptr;
257
Douglas Gregor9f782892013-01-21 15:25:38 +0000258 default:
259 llvm_unreachable("Unhandled DeclContext in AST reader");
260 }
261
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000262 llvm_unreachable("Unhandled decl kind");
Douglas Gregorfe732d52013-01-21 16:16:40 +0000263}
Douglas Gregor9f782892013-01-21 15:25:38 +0000264
Douglas Gregorfe732d52013-01-21 16:16:40 +0000265bool serialization::isRedeclarableDeclKind(unsigned Kind) {
266 switch (static_cast<Decl::Kind>(Kind)) {
Richard Smithf19e1272015-03-07 00:04:49 +0000267 case Decl::TranslationUnit:
268 case Decl::ExternCContext:
269 // Special case of a "merged" declaration.
270 return true;
271
Douglas Gregorfe732d52013-01-21 16:16:40 +0000272 case Decl::Namespace:
Richard Smithf4634362014-09-03 23:11:22 +0000273 case Decl::NamespaceAlias:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000274 case Decl::Typedef:
275 case Decl::TypeAlias:
276 case Decl::Enum:
277 case Decl::Record:
278 case Decl::CXXRecord:
279 case Decl::ClassTemplateSpecialization:
280 case Decl::ClassTemplatePartialSpecialization:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000281 case Decl::VarTemplateSpecialization:
282 case Decl::VarTemplatePartialSpecialization:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000283 case Decl::Function:
284 case Decl::CXXMethod:
285 case Decl::CXXConstructor:
286 case Decl::CXXDestructor:
287 case Decl::CXXConversion:
Richard Smithfd8634a2013-10-23 02:17:46 +0000288 case Decl::UsingShadow:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000289 case Decl::Var:
290 case Decl::FunctionTemplate:
291 case Decl::ClassTemplate:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000292 case Decl::VarTemplate:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000293 case Decl::TypeAliasTemplate:
294 case Decl::ObjCProtocol:
295 case Decl::ObjCInterface:
Michael Han84324352013-02-22 17:15:32 +0000296 case Decl::Empty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000297 return true;
298
299 // Never redeclarable.
300 case Decl::UsingDirective:
301 case Decl::Label:
302 case Decl::UnresolvedUsingTypename:
303 case Decl::TemplateTypeParm:
304 case Decl::EnumConstant:
305 case Decl::UnresolvedUsingValue:
306 case Decl::IndirectField:
307 case Decl::Field:
John McCall5e77d762013-04-16 07:28:30 +0000308 case Decl::MSProperty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000309 case Decl::ObjCIvar:
310 case Decl::ObjCAtDefsField:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000311 case Decl::NonTypeTemplateParm:
312 case Decl::TemplateTemplateParm:
313 case Decl::Using:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000314 case Decl::ObjCMethod:
315 case Decl::ObjCCategory:
316 case Decl::ObjCCategoryImpl:
317 case Decl::ObjCImplementation:
318 case Decl::ObjCProperty:
319 case Decl::ObjCCompatibleAlias:
320 case Decl::LinkageSpec:
321 case Decl::ObjCPropertyImpl:
322 case Decl::FileScopeAsm:
323 case Decl::AccessSpec:
324 case Decl::Friend:
325 case Decl::FriendTemplate:
326 case Decl::StaticAssert:
327 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000328 case Decl::Captured:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000329 case Decl::ClassScopeFunctionSpecialization:
330 case Decl::Import:
Alexey Bataeva769e072013-03-22 06:34:35 +0000331 case Decl::OMPThreadPrivate:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000332 return false;
Richard Smithf4634362014-09-03 23:11:22 +0000333
334 // These indirectly derive from Redeclarable<T> but are not actually
335 // redeclarable.
336 case Decl::ImplicitParam:
337 case Decl::ParmVar:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000338 case Decl::ObjCTypeParam:
Richard Smithf4634362014-09-03 23:11:22 +0000339 return false;
Douglas Gregorfe732d52013-01-21 16:16:40 +0000340 }
341
342 llvm_unreachable("Unhandled declaration kind");
Douglas Gregor9f782892013-01-21 15:25:38 +0000343}
Richard Smithd08aeb62014-08-28 01:33:39 +0000344
345bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
Richard Smith2b560572015-02-07 03:11:11 +0000346 // Friend declarations in dependent contexts aren't anonymous in the usual
347 // sense, but they cannot be found by name lookup in their semantic context
348 // (or indeed in any context), so we treat them as anonymous.
349 //
350 // This doesn't apply to friend tag decls; Sema makes those available to name
351 // lookup in the surrounding context.
352 if (D->getFriendObjectKind() &&
353 D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
354 // For function templates and class templates, the template is numbered and
355 // not its pattern.
356 if (auto *FD = dyn_cast<FunctionDecl>(D))
357 return !FD->getDescribedFunctionTemplate();
358 if (auto *RD = dyn_cast<CXXRecordDecl>(D))
359 return !RD->getDescribedClassTemplate();
360 return true;
361 }
362
363 // Otherwise, we only care about anonymous class members.
Richard Smithd08aeb62014-08-28 01:33:39 +0000364 if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
365 return false;
366 return isa<TagDecl>(D) || isa<FieldDecl>(D);
367}
368