blob: fcebe5982ffb2266aca97da86b0526d2217ec4cd [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"
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +000019#include "llvm/Support/DJB.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000020
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;
Leonard Chanf921d852018-06-04 16:07:52 +000094 case BuiltinType::ShortAccum:
95 ID = PREDEF_TYPE_SHORT_ACCUM_ID;
96 break;
97 case BuiltinType::Accum:
98 ID = PREDEF_TYPE_ACCUM_ID;
99 break;
100 case BuiltinType::LongAccum:
101 ID = PREDEF_TYPE_LONG_ACCUM_ID;
102 break;
103 case BuiltinType::UShortAccum:
104 ID = PREDEF_TYPE_USHORT_ACCUM_ID;
105 break;
106 case BuiltinType::UAccum:
107 ID = PREDEF_TYPE_UACCUM_ID;
108 break;
109 case BuiltinType::ULongAccum:
110 ID = PREDEF_TYPE_ULONG_ACCUM_ID;
111 break;
Leonard Chanab80f3c2018-06-14 14:53:51 +0000112 case BuiltinType::ShortFract:
113 ID = PREDEF_TYPE_SHORT_FRACT_ID;
114 break;
115 case BuiltinType::Fract:
116 ID = PREDEF_TYPE_FRACT_ID;
117 break;
118 case BuiltinType::LongFract:
119 ID = PREDEF_TYPE_LONG_FRACT_ID;
120 break;
121 case BuiltinType::UShortFract:
122 ID = PREDEF_TYPE_USHORT_FRACT_ID;
123 break;
124 case BuiltinType::UFract:
125 ID = PREDEF_TYPE_UFRACT_ID;
126 break;
127 case BuiltinType::ULongFract:
128 ID = PREDEF_TYPE_ULONG_FRACT_ID;
129 break;
130 case BuiltinType::SatShortAccum:
131 ID = PREDEF_TYPE_SAT_SHORT_ACCUM_ID;
132 break;
133 case BuiltinType::SatAccum:
134 ID = PREDEF_TYPE_SAT_ACCUM_ID;
135 break;
136 case BuiltinType::SatLongAccum:
137 ID = PREDEF_TYPE_SAT_LONG_ACCUM_ID;
138 break;
139 case BuiltinType::SatUShortAccum:
140 ID = PREDEF_TYPE_SAT_USHORT_ACCUM_ID;
141 break;
142 case BuiltinType::SatUAccum:
143 ID = PREDEF_TYPE_SAT_UACCUM_ID;
144 break;
145 case BuiltinType::SatULongAccum:
146 ID = PREDEF_TYPE_SAT_ULONG_ACCUM_ID;
147 break;
148 case BuiltinType::SatShortFract:
149 ID = PREDEF_TYPE_SAT_SHORT_FRACT_ID;
150 break;
151 case BuiltinType::SatFract:
152 ID = PREDEF_TYPE_SAT_FRACT_ID;
153 break;
154 case BuiltinType::SatLongFract:
155 ID = PREDEF_TYPE_SAT_LONG_FRACT_ID;
156 break;
157 case BuiltinType::SatUShortFract:
158 ID = PREDEF_TYPE_SAT_USHORT_FRACT_ID;
159 break;
160 case BuiltinType::SatUFract:
161 ID = PREDEF_TYPE_SAT_UFRACT_ID;
162 break;
163 case BuiltinType::SatULongFract:
164 ID = PREDEF_TYPE_SAT_ULONG_FRACT_ID;
165 break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000166 case BuiltinType::Float16:
167 ID = PREDEF_TYPE_FLOAT16_ID;
168 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000169 case BuiltinType::Float128:
170 ID = PREDEF_TYPE_FLOAT128_ID;
171 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +0000172 case BuiltinType::NullPtr:
173 ID = PREDEF_TYPE_NULLPTR_ID;
174 break;
Richard Smith3a8244d2018-05-01 05:02:45 +0000175 case BuiltinType::Char8:
176 ID = PREDEF_TYPE_CHAR8_ID;
177 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +0000178 case BuiltinType::Char16:
179 ID = PREDEF_TYPE_CHAR16_ID;
180 break;
181 case BuiltinType::Char32:
182 ID = PREDEF_TYPE_CHAR32_ID;
183 break;
184 case BuiltinType::Overload:
185 ID = PREDEF_TYPE_OVERLOAD_ID;
186 break;
187 case BuiltinType::BoundMember:
188 ID = PREDEF_TYPE_BOUND_MEMBER;
189 break;
190 case BuiltinType::PseudoObject:
191 ID = PREDEF_TYPE_PSEUDO_OBJECT;
192 break;
193 case BuiltinType::Dependent:
194 ID = PREDEF_TYPE_DEPENDENT_ID;
195 break;
196 case BuiltinType::UnknownAny:
197 ID = PREDEF_TYPE_UNKNOWN_ANY;
198 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000199 case BuiltinType::ARCUnbridgedCast:
Alexey Baderbdf7c842015-09-15 12:18:29 +0000200 ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;
201 break;
202 case BuiltinType::ObjCId:
203 ID = PREDEF_TYPE_OBJC_ID;
204 break;
205 case BuiltinType::ObjCClass:
206 ID = PREDEF_TYPE_OBJC_CLASS;
207 break;
208 case BuiltinType::ObjCSel:
209 ID = PREDEF_TYPE_OBJC_SEL;
210 break;
Alexey Bader954ba212016-04-08 13:40:33 +0000211#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
212 case BuiltinType::Id: \
213 ID = PREDEF_TYPE_##Id##_ID; \
Alexey Baderbdf7c842015-09-15 12:18:29 +0000214 break;
Alexey Baderb62f1442016-04-13 08:33:41 +0000215#include "clang/Basic/OpenCLImageTypes.def"
Alexey Baderbdf7c842015-09-15 12:18:29 +0000216 case BuiltinType::OCLSampler:
217 ID = PREDEF_TYPE_SAMPLER_ID;
218 break;
219 case BuiltinType::OCLEvent:
220 ID = PREDEF_TYPE_EVENT_ID;
221 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +0000222 case BuiltinType::OCLClkEvent:
223 ID = PREDEF_TYPE_CLK_EVENT_ID;
224 break;
225 case BuiltinType::OCLQueue:
226 ID = PREDEF_TYPE_QUEUE_ID;
227 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +0000228 case BuiltinType::OCLReserveID:
229 ID = PREDEF_TYPE_RESERVE_ID_ID;
230 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000231 case BuiltinType::BuiltinFn:
Alexey Baderbdf7c842015-09-15 12:18:29 +0000232 ID = PREDEF_TYPE_BUILTIN_FN;
233 break;
234 case BuiltinType::OMPArraySection:
235 ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
236 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000237 }
238
239 return TypeIdx(ID);
240}
241
242unsigned serialization::ComputeHash(Selector Sel) {
243 unsigned N = Sel.getNumArgs();
244 if (N == 0)
245 ++N;
246 unsigned R = 5381;
247 for (unsigned I = 0; I != N; ++I)
248 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000249 R = llvm::djbHash(II->getName(), R);
Guy Benyei11169dd2012-12-18 14:30:41 +0000250 return R;
251}
Douglas Gregor9f782892013-01-21 15:25:38 +0000252
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000253const DeclContext *
254serialization::getDefinitiveDeclContext(const DeclContext *DC) {
Douglas Gregor9f782892013-01-21 15:25:38 +0000255 switch (DC->getDeclKind()) {
256 // These entities may have multiple definitions.
257 case Decl::TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +0000258 case Decl::ExternCContext:
Douglas Gregor9f782892013-01-21 15:25:38 +0000259 case Decl::Namespace:
260 case Decl::LinkageSpec:
Richard Smith8df390f2016-09-08 23:14:54 +0000261 case Decl::Export:
Craig Toppera13603a2014-05-22 05:54:18 +0000262 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000263
264 // C/C++ tag types can only be defined in one place.
265 case Decl::Enum:
266 case Decl::Record:
267 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
268 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000269 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000270
271 // FIXME: These can be defined in one place... except special member
272 // functions and out-of-line definitions.
273 case Decl::CXXRecord:
274 case Decl::ClassTemplateSpecialization:
275 case Decl::ClassTemplatePartialSpecialization:
Craig Toppera13603a2014-05-22 05:54:18 +0000276 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000277
278 // Each function, method, and block declaration is its own DeclContext.
279 case Decl::Function:
280 case Decl::CXXMethod:
281 case Decl::CXXConstructor:
282 case Decl::CXXDestructor:
283 case Decl::CXXConversion:
284 case Decl::ObjCMethod:
285 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000286 case Decl::Captured:
Douglas Gregor9f782892013-01-21 15:25:38 +0000287 // Objective C categories, category implementations, and class
288 // implementations can only be defined in one place.
289 case Decl::ObjCCategory:
290 case Decl::ObjCCategoryImpl:
291 case Decl::ObjCImplementation:
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000292 return DC;
Douglas Gregor9f782892013-01-21 15:25:38 +0000293
294 case Decl::ObjCProtocol:
295 if (const ObjCProtocolDecl *Def
296 = cast<ObjCProtocolDecl>(DC)->getDefinition())
297 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000298 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000299
300 // FIXME: These are defined in one place, but properties in class extensions
301 // end up being back-patched into the main interface. See
302 // Sema::HandlePropertyInClassExtension for the offending code.
303 case Decl::ObjCInterface:
Craig Toppera13603a2014-05-22 05:54:18 +0000304 return nullptr;
305
Douglas Gregor9f782892013-01-21 15:25:38 +0000306 default:
307 llvm_unreachable("Unhandled DeclContext in AST reader");
308 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000309
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000310 llvm_unreachable("Unhandled decl kind");
Douglas Gregorfe732d52013-01-21 16:16:40 +0000311}
Douglas Gregor9f782892013-01-21 15:25:38 +0000312
Douglas Gregorfe732d52013-01-21 16:16:40 +0000313bool serialization::isRedeclarableDeclKind(unsigned Kind) {
314 switch (static_cast<Decl::Kind>(Kind)) {
Richard Smithf19e1272015-03-07 00:04:49 +0000315 case Decl::TranslationUnit:
316 case Decl::ExternCContext:
317 // Special case of a "merged" declaration.
318 return true;
319
Douglas Gregorfe732d52013-01-21 16:16:40 +0000320 case Decl::Namespace:
Richard Smithf4634362014-09-03 23:11:22 +0000321 case Decl::NamespaceAlias:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000322 case Decl::Typedef:
323 case Decl::TypeAlias:
324 case Decl::Enum:
325 case Decl::Record:
326 case Decl::CXXRecord:
327 case Decl::ClassTemplateSpecialization:
328 case Decl::ClassTemplatePartialSpecialization:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000329 case Decl::VarTemplateSpecialization:
330 case Decl::VarTemplatePartialSpecialization:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000331 case Decl::Function:
Richard Smithbc491202017-02-17 20:05:37 +0000332 case Decl::CXXDeductionGuide:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000333 case Decl::CXXMethod:
334 case Decl::CXXConstructor:
335 case Decl::CXXDestructor:
336 case Decl::CXXConversion:
Richard Smithfd8634a2013-10-23 02:17:46 +0000337 case Decl::UsingShadow:
Richard Smith5179eb72016-06-28 19:03:57 +0000338 case Decl::ConstructorUsingShadow:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000339 case Decl::Var:
340 case Decl::FunctionTemplate:
341 case Decl::ClassTemplate:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000342 case Decl::VarTemplate:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000343 case Decl::TypeAliasTemplate:
344 case Decl::ObjCProtocol:
345 case Decl::ObjCInterface:
Michael Han84324352013-02-22 17:15:32 +0000346 case Decl::Empty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000347 return true;
348
349 // Never redeclarable.
350 case Decl::UsingDirective:
351 case Decl::Label:
352 case Decl::UnresolvedUsingTypename:
353 case Decl::TemplateTypeParm:
354 case Decl::EnumConstant:
355 case Decl::UnresolvedUsingValue:
356 case Decl::IndirectField:
357 case Decl::Field:
John McCall5e77d762013-04-16 07:28:30 +0000358 case Decl::MSProperty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000359 case Decl::ObjCIvar:
360 case Decl::ObjCAtDefsField:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000361 case Decl::NonTypeTemplateParm:
362 case Decl::TemplateTemplateParm:
363 case Decl::Using:
Richard Smith151c4562016-12-20 21:35:28 +0000364 case Decl::UsingPack:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000365 case Decl::ObjCMethod:
366 case Decl::ObjCCategory:
367 case Decl::ObjCCategoryImpl:
368 case Decl::ObjCImplementation:
369 case Decl::ObjCProperty:
370 case Decl::ObjCCompatibleAlias:
371 case Decl::LinkageSpec:
Richard Smith8df390f2016-09-08 23:14:54 +0000372 case Decl::Export:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000373 case Decl::ObjCPropertyImpl:
Nico Weber66220292016-03-02 17:28:48 +0000374 case Decl::PragmaComment:
Nico Webercbbaeb12016-03-02 19:28:54 +0000375 case Decl::PragmaDetectMismatch:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000376 case Decl::FileScopeAsm:
377 case Decl::AccessSpec:
378 case Decl::Friend:
379 case Decl::FriendTemplate:
380 case Decl::StaticAssert:
381 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000382 case Decl::Captured:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000383 case Decl::ClassScopeFunctionSpecialization:
384 case Decl::Import:
Alexey Bataeva769e072013-03-22 06:34:35 +0000385 case Decl::OMPThreadPrivate:
Alexey Bataev4244be22016-02-11 05:35:55 +0000386 case Decl::OMPCapturedExpr:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000387 case Decl::OMPDeclareReduction:
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000388 case Decl::BuiltinTemplate:
Richard Smithbdb84f32016-07-22 23:36:59 +0000389 case Decl::Decomposition:
390 case Decl::Binding:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000391 return false;
Richard Smithf4634362014-09-03 23:11:22 +0000392
393 // These indirectly derive from Redeclarable<T> but are not actually
394 // redeclarable.
395 case Decl::ImplicitParam:
396 case Decl::ParmVar:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000397 case Decl::ObjCTypeParam:
Richard Smithf4634362014-09-03 23:11:22 +0000398 return false;
Douglas Gregorfe732d52013-01-21 16:16:40 +0000399 }
400
401 llvm_unreachable("Unhandled declaration kind");
Douglas Gregor9f782892013-01-21 15:25:38 +0000402}
Richard Smithd08aeb62014-08-28 01:33:39 +0000403
404bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
Richard Smith2b560572015-02-07 03:11:11 +0000405 // Friend declarations in dependent contexts aren't anonymous in the usual
406 // sense, but they cannot be found by name lookup in their semantic context
407 // (or indeed in any context), so we treat them as anonymous.
408 //
409 // This doesn't apply to friend tag decls; Sema makes those available to name
410 // lookup in the surrounding context.
411 if (D->getFriendObjectKind() &&
412 D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
413 // For function templates and class templates, the template is numbered and
414 // not its pattern.
415 if (auto *FD = dyn_cast<FunctionDecl>(D))
416 return !FD->getDescribedFunctionTemplate();
417 if (auto *RD = dyn_cast<CXXRecordDecl>(D))
418 return !RD->getDescribedClassTemplate();
419 return true;
420 }
421
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000422 // Otherwise, we only care about anonymous class members.
423 if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
Richard Smithd08aeb62014-08-28 01:33:39 +0000424 return false;
425 return isa<TagDecl>(D) || isa<FieldDecl>(D);
426}
427