clang 24.0.0git
ASTCommon.cpp
Go to the documentation of this file.
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"
18#include "llvm/Support/DJB.h"
19
20using namespace clang;
21
22// Give ASTDeserializationListener's VTable a home.
24
27 unsigned ID = 0;
28 switch (BT->getKind()) {
29 case BuiltinType::Void:
31 break;
32 case BuiltinType::Bool:
34 break;
35 case BuiltinType::Char_U:
37 break;
38 case BuiltinType::UChar:
40 break;
41 case BuiltinType::UShort:
43 break;
44 case BuiltinType::UInt:
46 break;
47 case BuiltinType::ULong:
49 break;
50 case BuiltinType::ULongLong:
52 break;
53 case BuiltinType::UInt128:
55 break;
56 case BuiltinType::Char_S:
58 break;
59 case BuiltinType::SChar:
61 break;
62 case BuiltinType::WChar_S:
63 case BuiltinType::WChar_U:
65 break;
66 case BuiltinType::Short:
68 break;
69 case BuiltinType::Int:
71 break;
72 case BuiltinType::Long:
74 break;
75 case BuiltinType::LongLong:
77 break;
78 case BuiltinType::Int128:
80 break;
81 case BuiltinType::Half:
83 break;
84 case BuiltinType::Float:
86 break;
87 case BuiltinType::Double:
89 break;
90 case BuiltinType::LongDouble:
92 break;
93 case BuiltinType::ShortAccum:
95 break;
96 case BuiltinType::Accum:
98 break;
99 case BuiltinType::LongAccum:
101 break;
102 case BuiltinType::UShortAccum:
104 break;
105 case BuiltinType::UAccum:
107 break;
108 case BuiltinType::ULongAccum:
110 break;
111 case BuiltinType::ShortFract:
113 break;
114 case BuiltinType::Fract:
116 break;
117 case BuiltinType::LongFract:
119 break;
120 case BuiltinType::UShortFract:
122 break;
123 case BuiltinType::UFract:
125 break;
126 case BuiltinType::ULongFract:
128 break;
129 case BuiltinType::SatShortAccum:
131 break;
132 case BuiltinType::SatAccum:
134 break;
135 case BuiltinType::SatLongAccum:
137 break;
138 case BuiltinType::SatUShortAccum:
140 break;
141 case BuiltinType::SatUAccum:
143 break;
144 case BuiltinType::SatULongAccum:
146 break;
147 case BuiltinType::SatShortFract:
149 break;
150 case BuiltinType::SatFract:
152 break;
153 case BuiltinType::SatLongFract:
155 break;
156 case BuiltinType::SatUShortFract:
158 break;
159 case BuiltinType::SatUFract:
161 break;
162 case BuiltinType::SatULongFract:
164 break;
165 case BuiltinType::Float16:
167 break;
168 case BuiltinType::Float128:
170 break;
171 case BuiltinType::Ibm128:
173 break;
174 case BuiltinType::NullPtr:
176 break;
177 case BuiltinType::Char8:
179 break;
180 case BuiltinType::Char16:
182 break;
183 case BuiltinType::Char32:
185 break;
186 case BuiltinType::Overload:
188 break;
189 case BuiltinType::UnresolvedTemplate:
191 break;
192 case BuiltinType::BoundMember:
194 break;
195 case BuiltinType::PseudoObject:
197 break;
198 case BuiltinType::Dependent:
200 break;
201 case BuiltinType::UnknownAny:
203 break;
204 case BuiltinType::ARCUnbridgedCast:
206 break;
207 case BuiltinType::ObjCId:
209 break;
210 case BuiltinType::ObjCClass:
212 break;
213 case BuiltinType::ObjCSel:
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:
228 break;
229 case BuiltinType::OCLEvent:
231 break;
232 case BuiltinType::OCLClkEvent:
234 break;
235 case BuiltinType::OCLQueue:
237 break;
238 case BuiltinType::OCLReserveID:
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/AArch64ACLETypes.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, Width, Align) \
262 case BuiltinType::Id: \
263 ID = PREDEF_TYPE_##Id##_ID; \
264 break;
265#include "clang/Basic/AMDGPUTypes.def"
266#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
267 case BuiltinType::Id: \
268 ID = PREDEF_TYPE_##Id##_ID; \
269 break;
270#include "clang/Basic/HLSLIntangibleTypes.def"
271#define SPIRV_TYPE(Name, Id, SingletonId) \
272 case BuiltinType::Id: \
273 ID = PREDEF_TYPE_##Id##_ID; \
274 break;
275#include "clang/Basic/SPIRVTypes.def"
276 case BuiltinType::BuiltinFn:
278 break;
279 case BuiltinType::IncompleteMatrixIdx:
281 break;
282 case BuiltinType::ArraySection:
284 break;
285 case BuiltinType::OMPArrayShaping:
287 break;
288 case BuiltinType::OMPIterator:
290 break;
291 case BuiltinType::BFloat16:
293 break;
294 }
295
296 return TypeIdx(0, ID);
297}
298
300 unsigned N = Sel.getNumArgs();
301 if (N == 0)
302 ++N;
303 unsigned R = 5381;
304 for (unsigned I = 0; I != N; ++I)
305 if (const IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
306 R = llvm::djbHash(II->getName(), R);
307 return R;
308}
309
310const DeclContext *
312 switch (DC->getDeclKind()) {
313 // These entities may have multiple definitions.
314 case Decl::TranslationUnit:
315 case Decl::ExternCContext:
316 case Decl::Namespace:
317 case Decl::LinkageSpec:
318 case Decl::Export:
319 return nullptr;
320
321 // C/C++ tag types can only be defined in one place.
322 case Decl::Enum:
323 case Decl::Record:
324 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
325 return Def;
326 return nullptr;
327
328 // FIXME: These can be defined in one place... except special member
329 // functions and out-of-line definitions.
330 case Decl::CXXRecord:
331 case Decl::ClassTemplateSpecialization:
332 case Decl::ClassTemplatePartialSpecialization:
333 return nullptr;
334
335 // Each function, method, and block declaration is its own DeclContext.
336 case Decl::Function:
337 case Decl::CXXMethod:
338 case Decl::CXXConstructor:
339 case Decl::CXXDestructor:
340 case Decl::CXXConversion:
341 case Decl::ObjCMethod:
342 case Decl::Block:
343 case Decl::OutlinedFunction:
344 case Decl::Captured:
345 // Objective C categories, category implementations, and class
346 // implementations can only be defined in one place.
347 case Decl::ObjCCategory:
348 case Decl::ObjCCategoryImpl:
349 case Decl::ObjCImplementation:
350 return DC;
351
352 case Decl::ObjCProtocol:
353 if (const ObjCProtocolDecl *Def
355 return Def;
356 return nullptr;
357
358 // FIXME: These are defined in one place, but properties in class extensions
359 // end up being back-patched into the main interface. See
360 // SemaObjC::HandlePropertyInClassExtension for the offending code.
361 case Decl::ObjCInterface:
362 return nullptr;
363
364 default:
365 llvm_unreachable("Unhandled DeclContext in AST reader");
366 }
367
368 llvm_unreachable("Unhandled decl kind");
369}
370
372 switch (static_cast<Decl::Kind>(Kind)) {
373 case Decl::TranslationUnit:
374 case Decl::ExternCContext:
375 // Special case of a "merged" declaration.
376 return true;
377
378 case Decl::Namespace:
379 case Decl::NamespaceAlias:
380 case Decl::Typedef:
381 case Decl::TypeAlias:
382 case Decl::Enum:
383 case Decl::Record:
384 case Decl::CXXRecord:
385 case Decl::ClassTemplateSpecialization:
386 case Decl::ClassTemplatePartialSpecialization:
387 case Decl::VarTemplateSpecialization:
388 case Decl::VarTemplatePartialSpecialization:
389 case Decl::Function:
390 case Decl::CXXDeductionGuide:
391 case Decl::CXXMethod:
392 case Decl::CXXConstructor:
393 case Decl::CXXDestructor:
394 case Decl::CXXConversion:
395 case Decl::UsingShadow:
396 case Decl::ConstructorUsingShadow:
397 case Decl::Var:
398 case Decl::FunctionTemplate:
399 case Decl::ClassTemplate:
400 case Decl::VarTemplate:
401 case Decl::TypeAliasTemplate:
402 case Decl::ObjCProtocol:
403 case Decl::ObjCInterface:
404 case Decl::Empty:
405 return true;
406
407 // Never redeclarable.
408 case Decl::ExplicitInstantiation:
409 case Decl::UsingDirective:
410 case Decl::Label:
411 case Decl::UnresolvedUsingTypename:
412 case Decl::TemplateTypeParm:
413 case Decl::EnumConstant:
414 case Decl::UnresolvedUsingValue:
415 case Decl::IndirectField:
416 case Decl::Field:
417 case Decl::MSProperty:
418 case Decl::MSGuid:
419 case Decl::UnnamedGlobalConstant:
420 case Decl::TemplateParamObject:
421 case Decl::ObjCIvar:
422 case Decl::ObjCAtDefsField:
423 case Decl::NonTypeTemplateParm:
424 case Decl::TemplateTemplateParm:
425 case Decl::Using:
426 case Decl::UsingEnum:
427 case Decl::UsingPack:
428 case Decl::ObjCMethod:
429 case Decl::ObjCCategory:
430 case Decl::ObjCCategoryImpl:
431 case Decl::ObjCImplementation:
432 case Decl::ObjCProperty:
433 case Decl::ObjCCompatibleAlias:
434 case Decl::LinkageSpec:
435 case Decl::Export:
436 case Decl::ObjCPropertyImpl:
437 case Decl::PragmaComment:
438 case Decl::PragmaDetectMismatch:
439 case Decl::FileScopeAsm:
440 case Decl::TopLevelStmt:
441 case Decl::AccessSpec:
442 case Decl::Friend:
443 case Decl::FriendTemplate:
444 case Decl::StaticAssert:
445 case Decl::Block:
446 case Decl::OutlinedFunction:
447 case Decl::Captured:
448 case Decl::Import:
449 case Decl::OMPThreadPrivate:
450 case Decl::OMPGroupPrivate:
451 case Decl::OMPAllocate:
452 case Decl::OMPRequires:
453 case Decl::OMPCapturedExpr:
454 case Decl::OMPDeclareReduction:
455 case Decl::OMPDeclareMapper:
456 case Decl::BuiltinTemplate:
457 case Decl::Decomposition:
458 case Decl::Binding:
459 case Decl::Concept:
460 case Decl::ImplicitConceptSpecialization:
461 case Decl::LifetimeExtendedTemporary:
462 case Decl::RequiresExprBody:
463 case Decl::UnresolvedUsingIfExists:
464 case Decl::HLSLBuffer:
465 case Decl::HLSLRootSignature:
466 case Decl::OpenACCDeclare:
467 case Decl::OpenACCRoutine:
468 case Decl::CXXExpansionStmt:
469 return false;
470
471 // These indirectly derive from Redeclarable<T> but are not actually
472 // redeclarable.
473 case Decl::ImplicitParam:
474 case Decl::ParmVar:
475 case Decl::ObjCTypeParam:
476 return false;
477 }
478
479 llvm_unreachable("Unhandled declaration kind");
480}
481
483 // Friend declarations in dependent contexts aren't anonymous in the usual
484 // sense, but they cannot be found by name lookup in their semantic context
485 // (or indeed in any context), so we treat them as anonymous.
486 //
487 // This doesn't apply to friend tag decls; Sema makes those available to name
488 // lookup in the surrounding context.
489 if (D->getFriendObjectKind() &&
491 // For function templates and class templates, the template is numbered and
492 // not its pattern.
493 if (auto *FD = dyn_cast<FunctionDecl>(D))
494 return !FD->getDescribedFunctionTemplate();
495 if (auto *RD = dyn_cast<CXXRecordDecl>(D))
496 return !RD->getDescribedClassTemplate();
497 return true;
498 }
499
500 // At block scope, we number everything that we need to deduplicate, since we
501 // can't just use name matching to keep things lined up.
502 // FIXME: This is only necessary for an inline function or
503 // a template specialization or similar.
505 // An uninstantiated template pattern is never emitted; only its
506 // instantiations are numbered, so its decls need no cross-module number.
508 return false;
509 if (auto *VD = dyn_cast<VarDecl>(D))
510 return VD->isStaticLocal();
511 // FIXME: What about CapturedDecls (and declarations nested within them)?
512 return isa<TagDecl>(D);
513 }
514
515 // Otherwise, we only care about anonymous class members / block-scope decls.
516 // FIXME: We need to handle blocks within inline / templated variables too.
517 if (D->getDeclName())
518 return false;
520 return false;
521 return isa<TagDecl, FieldDecl>(D);
522}
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
static const NamedDecl * getDefinition(const Decl *D)
This class is used for builtin types like 'int'.
Definition TypeBase.h:3241
Kind getKind() const
Definition TypeBase.h:3292
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1466
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
bool isFunctionOrMethod() const
Definition DeclBase.h:2178
Decl::Kind getDeclKind() const
Definition DeclBase.h:2119
FriendObjectKind getFriendObjectKind() const
Determines whether this declaration is the object of a friend declaration and, if so,...
Definition DeclBase.h:1243
Kind
Lists the kind of concrete classes of Decl.
Definition DeclBase.h:89
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC).
Definition DeclBase.h:935
One of these records is kept for each identifier that is lexed.
This represents a decl that may have a name.
Definition Decl.h:274
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:340
Represents an Objective-C protocol declaration.
Definition DeclObjC.h:2090
Smart pointer class that efficiently represents Objective-C method names.
const IdentifierInfo * getIdentifierInfoForSlot(unsigned argIndex) const
Retrieve the identifier at a given position in the selector.
unsigned getNumArgs() const
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3851
A type index; the type ID with the qualifier bits removed.
Definition ASTBitCodes.h:99
@ PREDEF_TYPE_LONG_ACCUM_ID
The 'long _Accum' type.
@ PREDEF_TYPE_SAMPLER_ID
OpenCL sampler type.
@ PREDEF_TYPE_INT128_ID
The '__int128_t' type.
@ PREDEF_TYPE_CHAR32_ID
The C++ 'char32_t' type.
@ PREDEF_TYPE_SAT_SHORT_ACCUM_ID
The '_Sat short _Accum' type.
@ PREDEF_TYPE_IBM128_ID
The '__ibm128' type.
@ PREDEF_TYPE_SHORT_FRACT_ID
The 'short _Fract' type.
@ PREDEF_TYPE_BOUND_MEMBER
The placeholder type for bound member functions.
@ PREDEF_TYPE_LONGLONG_ID
The (signed) 'long long' type.
@ PREDEF_TYPE_FRACT_ID
The '_Fract' type.
@ PREDEF_TYPE_ARC_UNBRIDGED_CAST
ARC's unbridged-cast placeholder type.
@ PREDEF_TYPE_USHORT_FRACT_ID
The 'unsigned short _Fract' type.
@ PREDEF_TYPE_SAT_ULONG_FRACT_ID
The '_Sat unsigned long _Fract' type.
@ PREDEF_TYPE_BOOL_ID
The 'bool' or '_Bool' type.
@ PREDEF_TYPE_SAT_LONG_ACCUM_ID
The '_Sat long _Accum' type.
@ PREDEF_TYPE_SAT_LONG_FRACT_ID
The '_Sat long _Fract' type.
@ PREDEF_TYPE_SAT_SHORT_FRACT_ID
The '_Sat short _Fract' type.
@ PREDEF_TYPE_CHAR_U_ID
The 'char' type, when it is unsigned.
@ PREDEF_TYPE_RESERVE_ID_ID
OpenCL reserve_id type.
@ PREDEF_TYPE_SAT_ACCUM_ID
The '_Sat _Accum' type.
@ PREDEF_TYPE_BUILTIN_FN
The placeholder type for builtin functions.
@ PREDEF_TYPE_SHORT_ACCUM_ID
The 'short _Accum' type.
@ PREDEF_TYPE_FLOAT_ID
The 'float' type.
@ PREDEF_TYPE_QUEUE_ID
OpenCL queue type.
@ PREDEF_TYPE_INT_ID
The (signed) 'int' type.
@ PREDEF_TYPE_OBJC_SEL
The ObjC 'SEL' type.
@ PREDEF_TYPE_BFLOAT16_ID
The '__bf16' type.
@ PREDEF_TYPE_WCHAR_ID
The C++ 'wchar_t' type.
@ PREDEF_TYPE_UCHAR_ID
The 'unsigned char' type.
@ PREDEF_TYPE_UACCUM_ID
The 'unsigned _Accum' type.
@ PREDEF_TYPE_SCHAR_ID
The 'signed char' type.
@ PREDEF_TYPE_CHAR_S_ID
The 'char' type, when it is signed.
@ PREDEF_TYPE_NULLPTR_ID
The type of 'nullptr'.
@ PREDEF_TYPE_ULONG_FRACT_ID
The 'unsigned long _Fract' type.
@ PREDEF_TYPE_FLOAT16_ID
The '_Float16' type.
@ PREDEF_TYPE_UINT_ID
The 'unsigned int' type.
@ PREDEF_TYPE_FLOAT128_ID
The '__float128' type.
@ PREDEF_TYPE_OBJC_ID
The ObjC 'id' type.
@ PREDEF_TYPE_CHAR16_ID
The C++ 'char16_t' type.
@ PREDEF_TYPE_ARRAY_SECTION
The placeholder type for an array section.
@ PREDEF_TYPE_ULONGLONG_ID
The 'unsigned long long' type.
@ PREDEF_TYPE_SAT_UFRACT_ID
The '_Sat unsigned _Fract' type.
@ PREDEF_TYPE_USHORT_ID
The 'unsigned short' type.
@ PREDEF_TYPE_SHORT_ID
The (signed) 'short' type.
@ PREDEF_TYPE_OMP_ARRAY_SHAPING
The placeholder type for OpenMP array shaping operation.
@ PREDEF_TYPE_DEPENDENT_ID
The placeholder type for dependent types.
@ PREDEF_TYPE_LONGDOUBLE_ID
The 'long double' type.
@ PREDEF_TYPE_DOUBLE_ID
The 'double' type.
@ PREDEF_TYPE_UINT128_ID
The '__uint128_t' type.
@ PREDEF_TYPE_HALF_ID
The OpenCL 'half' / ARM NEON __fp16 type.
@ PREDEF_TYPE_VOID_ID
The void type.
@ PREDEF_TYPE_SAT_USHORT_FRACT_ID
The '_Sat unsigned short _Fract' type.
@ PREDEF_TYPE_ACCUM_ID
The '_Accum' type.
@ PREDEF_TYPE_SAT_FRACT_ID
The '_Sat _Fract' type.
@ PREDEF_TYPE_USHORT_ACCUM_ID
The 'unsigned short _Accum' type.
@ PREDEF_TYPE_CHAR8_ID
The C++ 'char8_t' type.
@ PREDEF_TYPE_UFRACT_ID
The 'unsigned _Fract' type.
@ PREDEF_TYPE_OVERLOAD_ID
The placeholder type for overloaded function sets.
@ PREDEF_TYPE_INCOMPLETE_MATRIX_IDX
A placeholder type for incomplete matrix index operations.
@ PREDEF_TYPE_UNRESOLVED_TEMPLATE
The placeholder type for unresolved templates.
@ PREDEF_TYPE_SAT_USHORT_ACCUM_ID
The '_Sat unsigned short _Accum' type.
@ PREDEF_TYPE_LONG_ID
The (signed) 'long' type.
@ PREDEF_TYPE_SAT_ULONG_ACCUM_ID
The '_Sat unsigned long _Accum' type.
@ PREDEF_TYPE_LONG_FRACT_ID
The 'long _Fract' type.
@ PREDEF_TYPE_UNKNOWN_ANY
The 'unknown any' placeholder type.
@ PREDEF_TYPE_OMP_ITERATOR
The placeholder type for OpenMP iterator expression.
@ PREDEF_TYPE_PSEUDO_OBJECT
The pseudo-object placeholder type.
@ PREDEF_TYPE_OBJC_CLASS
The ObjC 'Class' type.
@ PREDEF_TYPE_ULONG_ID
The 'unsigned long' type.
@ PREDEF_TYPE_SAT_UACCUM_ID
The '_Sat unsigned _Accum' type.
@ PREDEF_TYPE_CLK_EVENT_ID
OpenCL clk event type.
@ PREDEF_TYPE_EVENT_ID
OpenCL event type.
@ PREDEF_TYPE_ULONG_ACCUM_ID
The 'unsigned long _Accum' type.
bool isRedeclarableDeclKind(unsigned Kind)
Determine whether the given declaration kind is redeclarable.
TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT)
Definition ASTCommon.cpp:26
bool needsAnonymousDeclarationNumber(const NamedDecl *D)
Determine whether the given declaration needs an anonymous declaration number.
const DeclContext * getDefinitiveDeclContext(const DeclContext *DC)
Retrieve the "definitive" declaration that provides all of the visible entries for the given declarat...
unsigned ComputeHash(Selector Sel)
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
U cast(CodeGen::Address addr)
Definition Address.h:327