1 //===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines common functions that both ASTReader and ASTWriter use.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "ASTCommon.h"
14 #include "clang/AST/DeclCXX.h"
15 #include "clang/AST/DeclObjC.h"
16 #include "clang/Basic/IdentifierTable.h"
17 #include "clang/Serialization/ASTDeserializationListener.h"
18 #include "llvm/Support/DJB.h"
19
20 using namespace clang;
21
22 // Give ASTDeserializationListener's VTable a home.
~ASTDeserializationListener()23 ASTDeserializationListener::~ASTDeserializationListener() { }
24
25 serialization::TypeIdx
TypeIdxFromBuiltin(const BuiltinType * BT)26 serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
27 unsigned ID = 0;
28 switch (BT->getKind()) {
29 case BuiltinType::Void:
30 ID = PREDEF_TYPE_VOID_ID;
31 break;
32 case BuiltinType::Bool:
33 ID = PREDEF_TYPE_BOOL_ID;
34 break;
35 case BuiltinType::Char_U:
36 ID = PREDEF_TYPE_CHAR_U_ID;
37 break;
38 case BuiltinType::UChar:
39 ID = PREDEF_TYPE_UCHAR_ID;
40 break;
41 case BuiltinType::UShort:
42 ID = PREDEF_TYPE_USHORT_ID;
43 break;
44 case BuiltinType::UInt:
45 ID = PREDEF_TYPE_UINT_ID;
46 break;
47 case BuiltinType::ULong:
48 ID = PREDEF_TYPE_ULONG_ID;
49 break;
50 case BuiltinType::ULongLong:
51 ID = PREDEF_TYPE_ULONGLONG_ID;
52 break;
53 case BuiltinType::UInt128:
54 ID = PREDEF_TYPE_UINT128_ID;
55 break;
56 case BuiltinType::Char_S:
57 ID = PREDEF_TYPE_CHAR_S_ID;
58 break;
59 case BuiltinType::SChar:
60 ID = PREDEF_TYPE_SCHAR_ID;
61 break;
62 case BuiltinType::WChar_S:
63 case BuiltinType::WChar_U:
64 ID = PREDEF_TYPE_WCHAR_ID;
65 break;
66 case BuiltinType::Short:
67 ID = PREDEF_TYPE_SHORT_ID;
68 break;
69 case BuiltinType::Int:
70 ID = PREDEF_TYPE_INT_ID;
71 break;
72 case BuiltinType::Long:
73 ID = PREDEF_TYPE_LONG_ID;
74 break;
75 case BuiltinType::LongLong:
76 ID = PREDEF_TYPE_LONGLONG_ID;
77 break;
78 case BuiltinType::Int128:
79 ID = PREDEF_TYPE_INT128_ID;
80 break;
81 case BuiltinType::Half:
82 ID = PREDEF_TYPE_HALF_ID;
83 break;
84 case BuiltinType::Float:
85 ID = PREDEF_TYPE_FLOAT_ID;
86 break;
87 case BuiltinType::Double:
88 ID = PREDEF_TYPE_DOUBLE_ID;
89 break;
90 case BuiltinType::LongDouble:
91 ID = PREDEF_TYPE_LONGDOUBLE_ID;
92 break;
93 case BuiltinType::ShortAccum:
94 ID = PREDEF_TYPE_SHORT_ACCUM_ID;
95 break;
96 case BuiltinType::Accum:
97 ID = PREDEF_TYPE_ACCUM_ID;
98 break;
99 case BuiltinType::LongAccum:
100 ID = PREDEF_TYPE_LONG_ACCUM_ID;
101 break;
102 case BuiltinType::UShortAccum:
103 ID = PREDEF_TYPE_USHORT_ACCUM_ID;
104 break;
105 case BuiltinType::UAccum:
106 ID = PREDEF_TYPE_UACCUM_ID;
107 break;
108 case BuiltinType::ULongAccum:
109 ID = PREDEF_TYPE_ULONG_ACCUM_ID;
110 break;
111 case BuiltinType::ShortFract:
112 ID = PREDEF_TYPE_SHORT_FRACT_ID;
113 break;
114 case BuiltinType::Fract:
115 ID = PREDEF_TYPE_FRACT_ID;
116 break;
117 case BuiltinType::LongFract:
118 ID = PREDEF_TYPE_LONG_FRACT_ID;
119 break;
120 case BuiltinType::UShortFract:
121 ID = PREDEF_TYPE_USHORT_FRACT_ID;
122 break;
123 case BuiltinType::UFract:
124 ID = PREDEF_TYPE_UFRACT_ID;
125 break;
126 case BuiltinType::ULongFract:
127 ID = PREDEF_TYPE_ULONG_FRACT_ID;
128 break;
129 case BuiltinType::SatShortAccum:
130 ID = PREDEF_TYPE_SAT_SHORT_ACCUM_ID;
131 break;
132 case BuiltinType::SatAccum:
133 ID = PREDEF_TYPE_SAT_ACCUM_ID;
134 break;
135 case BuiltinType::SatLongAccum:
136 ID = PREDEF_TYPE_SAT_LONG_ACCUM_ID;
137 break;
138 case BuiltinType::SatUShortAccum:
139 ID = PREDEF_TYPE_SAT_USHORT_ACCUM_ID;
140 break;
141 case BuiltinType::SatUAccum:
142 ID = PREDEF_TYPE_SAT_UACCUM_ID;
143 break;
144 case BuiltinType::SatULongAccum:
145 ID = PREDEF_TYPE_SAT_ULONG_ACCUM_ID;
146 break;
147 case BuiltinType::SatShortFract:
148 ID = PREDEF_TYPE_SAT_SHORT_FRACT_ID;
149 break;
150 case BuiltinType::SatFract:
151 ID = PREDEF_TYPE_SAT_FRACT_ID;
152 break;
153 case BuiltinType::SatLongFract:
154 ID = PREDEF_TYPE_SAT_LONG_FRACT_ID;
155 break;
156 case BuiltinType::SatUShortFract:
157 ID = PREDEF_TYPE_SAT_USHORT_FRACT_ID;
158 break;
159 case BuiltinType::SatUFract:
160 ID = PREDEF_TYPE_SAT_UFRACT_ID;
161 break;
162 case BuiltinType::SatULongFract:
163 ID = PREDEF_TYPE_SAT_ULONG_FRACT_ID;
164 break;
165 case BuiltinType::Float16:
166 ID = PREDEF_TYPE_FLOAT16_ID;
167 break;
168 case BuiltinType::Float128:
169 ID = PREDEF_TYPE_FLOAT128_ID;
170 break;
171 case BuiltinType::Ibm128:
172 ID = PREDEF_TYPE_IBM128_ID;
173 break;
174 case BuiltinType::NullPtr:
175 ID = PREDEF_TYPE_NULLPTR_ID;
176 break;
177 case BuiltinType::Char8:
178 ID = PREDEF_TYPE_CHAR8_ID;
179 break;
180 case BuiltinType::Char16:
181 ID = PREDEF_TYPE_CHAR16_ID;
182 break;
183 case BuiltinType::Char32:
184 ID = PREDEF_TYPE_CHAR32_ID;
185 break;
186 case BuiltinType::Overload:
187 ID = PREDEF_TYPE_OVERLOAD_ID;
188 break;
189 case BuiltinType::UnresolvedTemplate:
190 ID = PREDEF_TYPE_UNRESOLVED_TEMPLATE;
191 break;
192 case BuiltinType::BoundMember:
193 ID = PREDEF_TYPE_BOUND_MEMBER;
194 break;
195 case BuiltinType::PseudoObject:
196 ID = PREDEF_TYPE_PSEUDO_OBJECT;
197 break;
198 case BuiltinType::Dependent:
199 ID = PREDEF_TYPE_DEPENDENT_ID;
200 break;
201 case BuiltinType::UnknownAny:
202 ID = PREDEF_TYPE_UNKNOWN_ANY;
203 break;
204 case BuiltinType::ARCUnbridgedCast:
205 ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;
206 break;
207 case BuiltinType::ObjCId:
208 ID = PREDEF_TYPE_OBJC_ID;
209 break;
210 case BuiltinType::ObjCClass:
211 ID = PREDEF_TYPE_OBJC_CLASS;
212 break;
213 case BuiltinType::ObjCSel:
214 ID = PREDEF_TYPE_OBJC_SEL;
215 break;
216 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
217 case BuiltinType::Id: \
218 ID = PREDEF_TYPE_##Id##_ID; \
219 break;
220 #include "clang/Basic/OpenCLImageTypes.def"
221 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
222 case BuiltinType::Id: \
223 ID = PREDEF_TYPE_##Id##_ID; \
224 break;
225 #include "clang/Basic/OpenCLExtensionTypes.def"
226 case BuiltinType::OCLSampler:
227 ID = PREDEF_TYPE_SAMPLER_ID;
228 break;
229 case BuiltinType::OCLEvent:
230 ID = PREDEF_TYPE_EVENT_ID;
231 break;
232 case BuiltinType::OCLClkEvent:
233 ID = PREDEF_TYPE_CLK_EVENT_ID;
234 break;
235 case BuiltinType::OCLQueue:
236 ID = PREDEF_TYPE_QUEUE_ID;
237 break;
238 case BuiltinType::OCLReserveID:
239 ID = PREDEF_TYPE_RESERVE_ID_ID;
240 break;
241 #define SVE_TYPE(Name, Id, SingletonId) \
242 case BuiltinType::Id: \
243 ID = PREDEF_TYPE_##Id##_ID; \
244 break;
245 #include "clang/Basic/AArch64SVEACLETypes.def"
246 #define PPC_VECTOR_TYPE(Name, Id, Size) \
247 case BuiltinType::Id: \
248 ID = PREDEF_TYPE_##Id##_ID; \
249 break;
250 #include "clang/Basic/PPCTypes.def"
251 #define RVV_TYPE(Name, Id, SingletonId) \
252 case BuiltinType::Id: \
253 ID = PREDEF_TYPE_##Id##_ID; \
254 break;
255 #include "clang/Basic/RISCVVTypes.def"
256 #define WASM_TYPE(Name, Id, SingletonId) \
257 case BuiltinType::Id: \
258 ID = PREDEF_TYPE_##Id##_ID; \
259 break;
260 #include "clang/Basic/WebAssemblyReferenceTypes.def"
261 #define AMDGPU_TYPE(Name, Id, SingletonId) \
262 case BuiltinType::Id: \
263 ID = PREDEF_TYPE_##Id##_ID; \
264 break;
265 #include "clang/Basic/AMDGPUTypes.def"
266 case BuiltinType::BuiltinFn:
267 ID = PREDEF_TYPE_BUILTIN_FN;
268 break;
269 case BuiltinType::IncompleteMatrixIdx:
270 ID = PREDEF_TYPE_INCOMPLETE_MATRIX_IDX;
271 break;
272 case BuiltinType::ArraySection:
273 ID = PREDEF_TYPE_ARRAY_SECTION;
274 break;
275 case BuiltinType::OMPArrayShaping:
276 ID = PREDEF_TYPE_OMP_ARRAY_SHAPING;
277 break;
278 case BuiltinType::OMPIterator:
279 ID = PREDEF_TYPE_OMP_ITERATOR;
280 break;
281 case BuiltinType::BFloat16:
282 ID = PREDEF_TYPE_BFLOAT16_ID;
283 break;
284 }
285
286 return TypeIdx(0, ID);
287 }
288
ComputeHash(Selector Sel)289 unsigned serialization::ComputeHash(Selector Sel) {
290 unsigned N = Sel.getNumArgs();
291 if (N == 0)
292 ++N;
293 unsigned R = 5381;
294 for (unsigned I = 0; I != N; ++I)
295 if (const IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
296 R = llvm::djbHash(II->getName(), R);
297 return R;
298 }
299
300 const DeclContext *
getDefinitiveDeclContext(const DeclContext * DC)301 serialization::getDefinitiveDeclContext(const DeclContext *DC) {
302 switch (DC->getDeclKind()) {
303 // These entities may have multiple definitions.
304 case Decl::TranslationUnit:
305 case Decl::ExternCContext:
306 case Decl::Namespace:
307 case Decl::LinkageSpec:
308 case Decl::Export:
309 return nullptr;
310
311 // C/C++ tag types can only be defined in one place.
312 case Decl::Enum:
313 case Decl::Record:
314 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
315 return Def;
316 return nullptr;
317
318 // FIXME: These can be defined in one place... except special member
319 // functions and out-of-line definitions.
320 case Decl::CXXRecord:
321 case Decl::ClassTemplateSpecialization:
322 case Decl::ClassTemplatePartialSpecialization:
323 return nullptr;
324
325 // Each function, method, and block declaration is its own DeclContext.
326 case Decl::Function:
327 case Decl::CXXMethod:
328 case Decl::CXXConstructor:
329 case Decl::CXXDestructor:
330 case Decl::CXXConversion:
331 case Decl::ObjCMethod:
332 case Decl::Block:
333 case Decl::Captured:
334 // Objective C categories, category implementations, and class
335 // implementations can only be defined in one place.
336 case Decl::ObjCCategory:
337 case Decl::ObjCCategoryImpl:
338 case Decl::ObjCImplementation:
339 return DC;
340
341 case Decl::ObjCProtocol:
342 if (const ObjCProtocolDecl *Def
343 = cast<ObjCProtocolDecl>(DC)->getDefinition())
344 return Def;
345 return nullptr;
346
347 // FIXME: These are defined in one place, but properties in class extensions
348 // end up being back-patched into the main interface. See
349 // SemaObjC::HandlePropertyInClassExtension for the offending code.
350 case Decl::ObjCInterface:
351 return nullptr;
352
353 default:
354 llvm_unreachable("Unhandled DeclContext in AST reader");
355 }
356
357 llvm_unreachable("Unhandled decl kind");
358 }
359
isRedeclarableDeclKind(unsigned Kind)360 bool serialization::isRedeclarableDeclKind(unsigned Kind) {
361 switch (static_cast<Decl::Kind>(Kind)) {
362 case Decl::TranslationUnit:
363 case Decl::ExternCContext:
364 // Special case of a "merged" declaration.
365 return true;
366
367 case Decl::Namespace:
368 case Decl::NamespaceAlias:
369 case Decl::Typedef:
370 case Decl::TypeAlias:
371 case Decl::Enum:
372 case Decl::Record:
373 case Decl::CXXRecord:
374 case Decl::ClassTemplateSpecialization:
375 case Decl::ClassTemplatePartialSpecialization:
376 case Decl::VarTemplateSpecialization:
377 case Decl::VarTemplatePartialSpecialization:
378 case Decl::Function:
379 case Decl::CXXDeductionGuide:
380 case Decl::CXXMethod:
381 case Decl::CXXConstructor:
382 case Decl::CXXDestructor:
383 case Decl::CXXConversion:
384 case Decl::UsingShadow:
385 case Decl::ConstructorUsingShadow:
386 case Decl::Var:
387 case Decl::FunctionTemplate:
388 case Decl::ClassTemplate:
389 case Decl::VarTemplate:
390 case Decl::TypeAliasTemplate:
391 case Decl::ObjCProtocol:
392 case Decl::ObjCInterface:
393 case Decl::Empty:
394 return true;
395
396 // Never redeclarable.
397 case Decl::UsingDirective:
398 case Decl::Label:
399 case Decl::UnresolvedUsingTypename:
400 case Decl::TemplateTypeParm:
401 case Decl::EnumConstant:
402 case Decl::UnresolvedUsingValue:
403 case Decl::IndirectField:
404 case Decl::Field:
405 case Decl::MSProperty:
406 case Decl::MSGuid:
407 case Decl::UnnamedGlobalConstant:
408 case Decl::TemplateParamObject:
409 case Decl::ObjCIvar:
410 case Decl::ObjCAtDefsField:
411 case Decl::NonTypeTemplateParm:
412 case Decl::TemplateTemplateParm:
413 case Decl::Using:
414 case Decl::UsingEnum:
415 case Decl::UsingPack:
416 case Decl::ObjCMethod:
417 case Decl::ObjCCategory:
418 case Decl::ObjCCategoryImpl:
419 case Decl::ObjCImplementation:
420 case Decl::ObjCProperty:
421 case Decl::ObjCCompatibleAlias:
422 case Decl::LinkageSpec:
423 case Decl::Export:
424 case Decl::ObjCPropertyImpl:
425 case Decl::PragmaComment:
426 case Decl::PragmaDetectMismatch:
427 case Decl::FileScopeAsm:
428 case Decl::TopLevelStmt:
429 case Decl::AccessSpec:
430 case Decl::Friend:
431 case Decl::FriendTemplate:
432 case Decl::StaticAssert:
433 case Decl::Block:
434 case Decl::Captured:
435 case Decl::Import:
436 case Decl::OMPThreadPrivate:
437 case Decl::OMPAllocate:
438 case Decl::OMPRequires:
439 case Decl::OMPCapturedExpr:
440 case Decl::OMPDeclareReduction:
441 case Decl::OMPDeclareMapper:
442 case Decl::BuiltinTemplate:
443 case Decl::Decomposition:
444 case Decl::Binding:
445 case Decl::Concept:
446 case Decl::ImplicitConceptSpecialization:
447 case Decl::LifetimeExtendedTemporary:
448 case Decl::RequiresExprBody:
449 case Decl::UnresolvedUsingIfExists:
450 case Decl::HLSLBuffer:
451 return false;
452
453 // These indirectly derive from Redeclarable<T> but are not actually
454 // redeclarable.
455 case Decl::ImplicitParam:
456 case Decl::ParmVar:
457 case Decl::ObjCTypeParam:
458 return false;
459 }
460
461 llvm_unreachable("Unhandled declaration kind");
462 }
463
needsAnonymousDeclarationNumber(const NamedDecl * D)464 bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
465 // Friend declarations in dependent contexts aren't anonymous in the usual
466 // sense, but they cannot be found by name lookup in their semantic context
467 // (or indeed in any context), so we treat them as anonymous.
468 //
469 // This doesn't apply to friend tag decls; Sema makes those available to name
470 // lookup in the surrounding context.
471 if (D->getFriendObjectKind() &&
472 D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
473 // For function templates and class templates, the template is numbered and
474 // not its pattern.
475 if (auto *FD = dyn_cast<FunctionDecl>(D))
476 return !FD->getDescribedFunctionTemplate();
477 if (auto *RD = dyn_cast<CXXRecordDecl>(D))
478 return !RD->getDescribedClassTemplate();
479 return true;
480 }
481
482 // At block scope, we number everything that we need to deduplicate, since we
483 // can't just use name matching to keep things lined up.
484 // FIXME: This is only necessary for an inline function or a template or
485 // similar.
486 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
487 if (auto *VD = dyn_cast<VarDecl>(D))
488 return VD->isStaticLocal();
489 // FIXME: What about CapturedDecls (and declarations nested within them)?
490 return isa<TagDecl, BlockDecl>(D);
491 }
492
493 // Otherwise, we only care about anonymous class members / block-scope decls.
494 // FIXME: We need to handle blocks within inline / templated variables too.
495 if (D->getDeclName())
496 return false;
497 if (!isa<RecordDecl, ObjCInterfaceDecl>(D->getLexicalDeclContext()))
498 return false;
499 return isa<TagDecl, FieldDecl>(D);
500 }
501