blob: 76630f5e8ad154d580676f7bbe76a5c7dfe48986 [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;
Alexey Bader954ba212016-04-08 13:40:33 +0000130#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
131 case BuiltinType::Id: \
132 ID = PREDEF_TYPE_##Id##_ID; \
Alexey Baderbdf7c842015-09-15 12:18:29 +0000133 break;
Alexey Bader954ba212016-04-08 13:40:33 +0000134#include "clang/AST/OpenCLImageTypes.def"
Alexey Baderbdf7c842015-09-15 12:18:29 +0000135 case BuiltinType::OCLSampler:
136 ID = PREDEF_TYPE_SAMPLER_ID;
137 break;
138 case BuiltinType::OCLEvent:
139 ID = PREDEF_TYPE_EVENT_ID;
140 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +0000141 case BuiltinType::OCLClkEvent:
142 ID = PREDEF_TYPE_CLK_EVENT_ID;
143 break;
144 case BuiltinType::OCLQueue:
145 ID = PREDEF_TYPE_QUEUE_ID;
146 break;
147 case BuiltinType::OCLNDRange:
148 ID = PREDEF_TYPE_NDRANGE_ID;
149 break;
150 case BuiltinType::OCLReserveID:
151 ID = PREDEF_TYPE_RESERVE_ID_ID;
152 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000153 case BuiltinType::BuiltinFn:
Alexey Baderbdf7c842015-09-15 12:18:29 +0000154 ID = PREDEF_TYPE_BUILTIN_FN;
155 break;
156 case BuiltinType::OMPArraySection:
157 ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
158 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000159 }
160
161 return TypeIdx(ID);
162}
163
164unsigned serialization::ComputeHash(Selector Sel) {
165 unsigned N = Sel.getNumArgs();
166 if (N == 0)
167 ++N;
168 unsigned R = 5381;
169 for (unsigned I = 0; I != N; ++I)
170 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
171 R = llvm::HashString(II->getName(), R);
172 return R;
173}
Douglas Gregor9f782892013-01-21 15:25:38 +0000174
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000175const DeclContext *
176serialization::getDefinitiveDeclContext(const DeclContext *DC) {
Douglas Gregor9f782892013-01-21 15:25:38 +0000177 switch (DC->getDeclKind()) {
178 // These entities may have multiple definitions.
179 case Decl::TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +0000180 case Decl::ExternCContext:
Douglas Gregor9f782892013-01-21 15:25:38 +0000181 case Decl::Namespace:
182 case Decl::LinkageSpec:
Craig Toppera13603a2014-05-22 05:54:18 +0000183 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000184
185 // C/C++ tag types can only be defined in one place.
186 case Decl::Enum:
187 case Decl::Record:
188 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
189 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000190 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000191
192 // FIXME: These can be defined in one place... except special member
193 // functions and out-of-line definitions.
194 case Decl::CXXRecord:
195 case Decl::ClassTemplateSpecialization:
196 case Decl::ClassTemplatePartialSpecialization:
Craig Toppera13603a2014-05-22 05:54:18 +0000197 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000198
199 // Each function, method, and block declaration is its own DeclContext.
200 case Decl::Function:
201 case Decl::CXXMethod:
202 case Decl::CXXConstructor:
203 case Decl::CXXDestructor:
204 case Decl::CXXConversion:
205 case Decl::ObjCMethod:
206 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000207 case Decl::Captured:
Douglas Gregor9f782892013-01-21 15:25:38 +0000208 // Objective C categories, category implementations, and class
209 // implementations can only be defined in one place.
210 case Decl::ObjCCategory:
211 case Decl::ObjCCategoryImpl:
212 case Decl::ObjCImplementation:
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000213 return DC;
Douglas Gregor9f782892013-01-21 15:25:38 +0000214
215 case Decl::ObjCProtocol:
216 if (const ObjCProtocolDecl *Def
217 = cast<ObjCProtocolDecl>(DC)->getDefinition())
218 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000219 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000220
221 // FIXME: These are defined in one place, but properties in class extensions
222 // end up being back-patched into the main interface. See
223 // Sema::HandlePropertyInClassExtension for the offending code.
224 case Decl::ObjCInterface:
Craig Toppera13603a2014-05-22 05:54:18 +0000225 return nullptr;
226
Douglas Gregor9f782892013-01-21 15:25:38 +0000227 default:
228 llvm_unreachable("Unhandled DeclContext in AST reader");
229 }
230
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000231 llvm_unreachable("Unhandled decl kind");
Douglas Gregorfe732d52013-01-21 16:16:40 +0000232}
Douglas Gregor9f782892013-01-21 15:25:38 +0000233
Douglas Gregorfe732d52013-01-21 16:16:40 +0000234bool serialization::isRedeclarableDeclKind(unsigned Kind) {
235 switch (static_cast<Decl::Kind>(Kind)) {
Richard Smithf19e1272015-03-07 00:04:49 +0000236 case Decl::TranslationUnit:
237 case Decl::ExternCContext:
238 // Special case of a "merged" declaration.
239 return true;
240
Douglas Gregorfe732d52013-01-21 16:16:40 +0000241 case Decl::Namespace:
Richard Smithf4634362014-09-03 23:11:22 +0000242 case Decl::NamespaceAlias:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000243 case Decl::Typedef:
244 case Decl::TypeAlias:
245 case Decl::Enum:
246 case Decl::Record:
247 case Decl::CXXRecord:
248 case Decl::ClassTemplateSpecialization:
249 case Decl::ClassTemplatePartialSpecialization:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000250 case Decl::VarTemplateSpecialization:
251 case Decl::VarTemplatePartialSpecialization:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000252 case Decl::Function:
253 case Decl::CXXMethod:
254 case Decl::CXXConstructor:
255 case Decl::CXXDestructor:
256 case Decl::CXXConversion:
Richard Smithfd8634a2013-10-23 02:17:46 +0000257 case Decl::UsingShadow:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000258 case Decl::Var:
259 case Decl::FunctionTemplate:
260 case Decl::ClassTemplate:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000261 case Decl::VarTemplate:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000262 case Decl::TypeAliasTemplate:
263 case Decl::ObjCProtocol:
264 case Decl::ObjCInterface:
Michael Han84324352013-02-22 17:15:32 +0000265 case Decl::Empty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000266 return true;
267
268 // Never redeclarable.
269 case Decl::UsingDirective:
270 case Decl::Label:
271 case Decl::UnresolvedUsingTypename:
272 case Decl::TemplateTypeParm:
273 case Decl::EnumConstant:
274 case Decl::UnresolvedUsingValue:
275 case Decl::IndirectField:
276 case Decl::Field:
John McCall5e77d762013-04-16 07:28:30 +0000277 case Decl::MSProperty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000278 case Decl::ObjCIvar:
279 case Decl::ObjCAtDefsField:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000280 case Decl::NonTypeTemplateParm:
281 case Decl::TemplateTemplateParm:
282 case Decl::Using:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000283 case Decl::ObjCMethod:
284 case Decl::ObjCCategory:
285 case Decl::ObjCCategoryImpl:
286 case Decl::ObjCImplementation:
287 case Decl::ObjCProperty:
288 case Decl::ObjCCompatibleAlias:
289 case Decl::LinkageSpec:
290 case Decl::ObjCPropertyImpl:
Nico Weber66220292016-03-02 17:28:48 +0000291 case Decl::PragmaComment:
Nico Webercbbaeb12016-03-02 19:28:54 +0000292 case Decl::PragmaDetectMismatch:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000293 case Decl::FileScopeAsm:
294 case Decl::AccessSpec:
295 case Decl::Friend:
296 case Decl::FriendTemplate:
297 case Decl::StaticAssert:
298 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000299 case Decl::Captured:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000300 case Decl::ClassScopeFunctionSpecialization:
301 case Decl::Import:
Alexey Bataeva769e072013-03-22 06:34:35 +0000302 case Decl::OMPThreadPrivate:
Alexey Bataev4244be22016-02-11 05:35:55 +0000303 case Decl::OMPCapturedExpr:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000304 case Decl::OMPDeclareReduction:
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000305 case Decl::BuiltinTemplate:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000306 return false;
Richard Smithf4634362014-09-03 23:11:22 +0000307
308 // These indirectly derive from Redeclarable<T> but are not actually
309 // redeclarable.
310 case Decl::ImplicitParam:
311 case Decl::ParmVar:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000312 case Decl::ObjCTypeParam:
Richard Smithf4634362014-09-03 23:11:22 +0000313 return false;
Douglas Gregorfe732d52013-01-21 16:16:40 +0000314 }
315
316 llvm_unreachable("Unhandled declaration kind");
Douglas Gregor9f782892013-01-21 15:25:38 +0000317}
Richard Smithd08aeb62014-08-28 01:33:39 +0000318
319bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
Richard Smith2b560572015-02-07 03:11:11 +0000320 // Friend declarations in dependent contexts aren't anonymous in the usual
321 // sense, but they cannot be found by name lookup in their semantic context
322 // (or indeed in any context), so we treat them as anonymous.
323 //
324 // This doesn't apply to friend tag decls; Sema makes those available to name
325 // lookup in the surrounding context.
326 if (D->getFriendObjectKind() &&
327 D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
328 // For function templates and class templates, the template is numbered and
329 // not its pattern.
330 if (auto *FD = dyn_cast<FunctionDecl>(D))
331 return !FD->getDescribedFunctionTemplate();
332 if (auto *RD = dyn_cast<CXXRecordDecl>(D))
333 return !RD->getDescribedClassTemplate();
334 return true;
335 }
336
337 // Otherwise, we only care about anonymous class members.
Richard Smithd08aeb62014-08-28 01:33:39 +0000338 if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
339 return false;
340 return isa<TagDecl>(D) || isa<FieldDecl>(D);
341}
342