blob: 535aacb8c499558523afa525410bb768a5da7b54 [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;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +000094 case BuiltinType::Float16:
95 ID = PREDEF_TYPE_FLOAT16_ID;
96 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +000097 case BuiltinType::Float128:
98 ID = PREDEF_TYPE_FLOAT128_ID;
99 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +0000100 case BuiltinType::NullPtr:
101 ID = PREDEF_TYPE_NULLPTR_ID;
102 break;
103 case BuiltinType::Char16:
104 ID = PREDEF_TYPE_CHAR16_ID;
105 break;
106 case BuiltinType::Char32:
107 ID = PREDEF_TYPE_CHAR32_ID;
108 break;
109 case BuiltinType::Overload:
110 ID = PREDEF_TYPE_OVERLOAD_ID;
111 break;
112 case BuiltinType::BoundMember:
113 ID = PREDEF_TYPE_BOUND_MEMBER;
114 break;
115 case BuiltinType::PseudoObject:
116 ID = PREDEF_TYPE_PSEUDO_OBJECT;
117 break;
118 case BuiltinType::Dependent:
119 ID = PREDEF_TYPE_DEPENDENT_ID;
120 break;
121 case BuiltinType::UnknownAny:
122 ID = PREDEF_TYPE_UNKNOWN_ANY;
123 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000124 case BuiltinType::ARCUnbridgedCast:
Alexey Baderbdf7c842015-09-15 12:18:29 +0000125 ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;
126 break;
127 case BuiltinType::ObjCId:
128 ID = PREDEF_TYPE_OBJC_ID;
129 break;
130 case BuiltinType::ObjCClass:
131 ID = PREDEF_TYPE_OBJC_CLASS;
132 break;
133 case BuiltinType::ObjCSel:
134 ID = PREDEF_TYPE_OBJC_SEL;
135 break;
Alexey Bader954ba212016-04-08 13:40:33 +0000136#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
137 case BuiltinType::Id: \
138 ID = PREDEF_TYPE_##Id##_ID; \
Alexey Baderbdf7c842015-09-15 12:18:29 +0000139 break;
Alexey Baderb62f1442016-04-13 08:33:41 +0000140#include "clang/Basic/OpenCLImageTypes.def"
Alexey Baderbdf7c842015-09-15 12:18:29 +0000141 case BuiltinType::OCLSampler:
142 ID = PREDEF_TYPE_SAMPLER_ID;
143 break;
144 case BuiltinType::OCLEvent:
145 ID = PREDEF_TYPE_EVENT_ID;
146 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +0000147 case BuiltinType::OCLClkEvent:
148 ID = PREDEF_TYPE_CLK_EVENT_ID;
149 break;
150 case BuiltinType::OCLQueue:
151 ID = PREDEF_TYPE_QUEUE_ID;
152 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +0000153 case BuiltinType::OCLReserveID:
154 ID = PREDEF_TYPE_RESERVE_ID_ID;
155 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000156 case BuiltinType::BuiltinFn:
Alexey Baderbdf7c842015-09-15 12:18:29 +0000157 ID = PREDEF_TYPE_BUILTIN_FN;
158 break;
159 case BuiltinType::OMPArraySection:
160 ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
161 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000162 }
163
164 return TypeIdx(ID);
165}
166
167unsigned serialization::ComputeHash(Selector Sel) {
168 unsigned N = Sel.getNumArgs();
169 if (N == 0)
170 ++N;
171 unsigned R = 5381;
172 for (unsigned I = 0; I != N; ++I)
173 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000174 R = llvm::djbHash(II->getName(), R);
Guy Benyei11169dd2012-12-18 14:30:41 +0000175 return R;
176}
Douglas Gregor9f782892013-01-21 15:25:38 +0000177
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000178const DeclContext *
179serialization::getDefinitiveDeclContext(const DeclContext *DC) {
Douglas Gregor9f782892013-01-21 15:25:38 +0000180 switch (DC->getDeclKind()) {
181 // These entities may have multiple definitions.
182 case Decl::TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +0000183 case Decl::ExternCContext:
Douglas Gregor9f782892013-01-21 15:25:38 +0000184 case Decl::Namespace:
185 case Decl::LinkageSpec:
Richard Smith8df390f2016-09-08 23:14:54 +0000186 case Decl::Export:
Craig Toppera13603a2014-05-22 05:54:18 +0000187 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000188
189 // C/C++ tag types can only be defined in one place.
190 case Decl::Enum:
191 case Decl::Record:
192 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
193 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000194 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000195
196 // FIXME: These can be defined in one place... except special member
197 // functions and out-of-line definitions.
198 case Decl::CXXRecord:
199 case Decl::ClassTemplateSpecialization:
200 case Decl::ClassTemplatePartialSpecialization:
Craig Toppera13603a2014-05-22 05:54:18 +0000201 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000202
203 // Each function, method, and block declaration is its own DeclContext.
204 case Decl::Function:
205 case Decl::CXXMethod:
206 case Decl::CXXConstructor:
207 case Decl::CXXDestructor:
208 case Decl::CXXConversion:
209 case Decl::ObjCMethod:
210 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000211 case Decl::Captured:
Douglas Gregor9f782892013-01-21 15:25:38 +0000212 // Objective C categories, category implementations, and class
213 // implementations can only be defined in one place.
214 case Decl::ObjCCategory:
215 case Decl::ObjCCategoryImpl:
216 case Decl::ObjCImplementation:
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000217 return DC;
Douglas Gregor9f782892013-01-21 15:25:38 +0000218
219 case Decl::ObjCProtocol:
220 if (const ObjCProtocolDecl *Def
221 = cast<ObjCProtocolDecl>(DC)->getDefinition())
222 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000223 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000224
225 // FIXME: These are defined in one place, but properties in class extensions
226 // end up being back-patched into the main interface. See
227 // Sema::HandlePropertyInClassExtension for the offending code.
228 case Decl::ObjCInterface:
Craig Toppera13603a2014-05-22 05:54:18 +0000229 return nullptr;
230
Douglas Gregor9f782892013-01-21 15:25:38 +0000231 default:
232 llvm_unreachable("Unhandled DeclContext in AST reader");
233 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000234
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000235 llvm_unreachable("Unhandled decl kind");
Douglas Gregorfe732d52013-01-21 16:16:40 +0000236}
Douglas Gregor9f782892013-01-21 15:25:38 +0000237
Douglas Gregorfe732d52013-01-21 16:16:40 +0000238bool serialization::isRedeclarableDeclKind(unsigned Kind) {
239 switch (static_cast<Decl::Kind>(Kind)) {
Richard Smithf19e1272015-03-07 00:04:49 +0000240 case Decl::TranslationUnit:
241 case Decl::ExternCContext:
242 // Special case of a "merged" declaration.
243 return true;
244
Douglas Gregorfe732d52013-01-21 16:16:40 +0000245 case Decl::Namespace:
Richard Smithf4634362014-09-03 23:11:22 +0000246 case Decl::NamespaceAlias:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000247 case Decl::Typedef:
248 case Decl::TypeAlias:
249 case Decl::Enum:
250 case Decl::Record:
251 case Decl::CXXRecord:
252 case Decl::ClassTemplateSpecialization:
253 case Decl::ClassTemplatePartialSpecialization:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000254 case Decl::VarTemplateSpecialization:
255 case Decl::VarTemplatePartialSpecialization:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000256 case Decl::Function:
Richard Smithbc491202017-02-17 20:05:37 +0000257 case Decl::CXXDeductionGuide:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000258 case Decl::CXXMethod:
259 case Decl::CXXConstructor:
260 case Decl::CXXDestructor:
261 case Decl::CXXConversion:
Richard Smithfd8634a2013-10-23 02:17:46 +0000262 case Decl::UsingShadow:
Richard Smith5179eb72016-06-28 19:03:57 +0000263 case Decl::ConstructorUsingShadow:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000264 case Decl::Var:
265 case Decl::FunctionTemplate:
266 case Decl::ClassTemplate:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000267 case Decl::VarTemplate:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000268 case Decl::TypeAliasTemplate:
269 case Decl::ObjCProtocol:
270 case Decl::ObjCInterface:
Michael Han84324352013-02-22 17:15:32 +0000271 case Decl::Empty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000272 return true;
273
274 // Never redeclarable.
275 case Decl::UsingDirective:
276 case Decl::Label:
277 case Decl::UnresolvedUsingTypename:
278 case Decl::TemplateTypeParm:
279 case Decl::EnumConstant:
280 case Decl::UnresolvedUsingValue:
281 case Decl::IndirectField:
282 case Decl::Field:
John McCall5e77d762013-04-16 07:28:30 +0000283 case Decl::MSProperty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000284 case Decl::ObjCIvar:
285 case Decl::ObjCAtDefsField:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000286 case Decl::NonTypeTemplateParm:
287 case Decl::TemplateTemplateParm:
288 case Decl::Using:
Richard Smith151c4562016-12-20 21:35:28 +0000289 case Decl::UsingPack:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000290 case Decl::ObjCMethod:
291 case Decl::ObjCCategory:
292 case Decl::ObjCCategoryImpl:
293 case Decl::ObjCImplementation:
294 case Decl::ObjCProperty:
295 case Decl::ObjCCompatibleAlias:
296 case Decl::LinkageSpec:
Richard Smith8df390f2016-09-08 23:14:54 +0000297 case Decl::Export:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000298 case Decl::ObjCPropertyImpl:
Nico Weber66220292016-03-02 17:28:48 +0000299 case Decl::PragmaComment:
Nico Webercbbaeb12016-03-02 19:28:54 +0000300 case Decl::PragmaDetectMismatch:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000301 case Decl::FileScopeAsm:
302 case Decl::AccessSpec:
303 case Decl::Friend:
304 case Decl::FriendTemplate:
305 case Decl::StaticAssert:
306 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000307 case Decl::Captured:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000308 case Decl::ClassScopeFunctionSpecialization:
309 case Decl::Import:
Alexey Bataeva769e072013-03-22 06:34:35 +0000310 case Decl::OMPThreadPrivate:
Alexey Bataev4244be22016-02-11 05:35:55 +0000311 case Decl::OMPCapturedExpr:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000312 case Decl::OMPDeclareReduction:
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000313 case Decl::BuiltinTemplate:
Richard Smithbdb84f32016-07-22 23:36:59 +0000314 case Decl::Decomposition:
315 case Decl::Binding:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000316 return false;
Richard Smithf4634362014-09-03 23:11:22 +0000317
318 // These indirectly derive from Redeclarable<T> but are not actually
319 // redeclarable.
320 case Decl::ImplicitParam:
321 case Decl::ParmVar:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000322 case Decl::ObjCTypeParam:
Richard Smithf4634362014-09-03 23:11:22 +0000323 return false;
Douglas Gregorfe732d52013-01-21 16:16:40 +0000324 }
325
326 llvm_unreachable("Unhandled declaration kind");
Douglas Gregor9f782892013-01-21 15:25:38 +0000327}
Richard Smithd08aeb62014-08-28 01:33:39 +0000328
329bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
Richard Smith2b560572015-02-07 03:11:11 +0000330 // Friend declarations in dependent contexts aren't anonymous in the usual
331 // sense, but they cannot be found by name lookup in their semantic context
332 // (or indeed in any context), so we treat them as anonymous.
333 //
334 // This doesn't apply to friend tag decls; Sema makes those available to name
335 // lookup in the surrounding context.
336 if (D->getFriendObjectKind() &&
337 D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
338 // For function templates and class templates, the template is numbered and
339 // not its pattern.
340 if (auto *FD = dyn_cast<FunctionDecl>(D))
341 return !FD->getDescribedFunctionTemplate();
342 if (auto *RD = dyn_cast<CXXRecordDecl>(D))
343 return !RD->getDescribedClassTemplate();
344 return true;
345 }
346
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000347 // Otherwise, we only care about anonymous class members.
348 if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
Richard Smithd08aeb62014-08-28 01:33:39 +0000349 return false;
350 return isa<TagDecl>(D) || isa<FieldDecl>(D);
351}
352