xref: /freebsd/contrib/llvm-project/clang/lib/Serialization/ASTCommon.cpp (revision acb1f1269c6f4ff89a0d28ba742f6687e9ef779d)
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.
23 ASTDeserializationListener::~ASTDeserializationListener() { }
24 
25 serialization::TypeIdx
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::NullPtr:
172     ID = PREDEF_TYPE_NULLPTR_ID;
173     break;
174   case BuiltinType::Char8:
175     ID = PREDEF_TYPE_CHAR8_ID;
176     break;
177   case BuiltinType::Char16:
178     ID = PREDEF_TYPE_CHAR16_ID;
179     break;
180   case BuiltinType::Char32:
181     ID = PREDEF_TYPE_CHAR32_ID;
182     break;
183   case BuiltinType::Overload:
184     ID = PREDEF_TYPE_OVERLOAD_ID;
185     break;
186   case BuiltinType::BoundMember:
187     ID = PREDEF_TYPE_BOUND_MEMBER;
188     break;
189   case BuiltinType::PseudoObject:
190     ID = PREDEF_TYPE_PSEUDO_OBJECT;
191     break;
192   case BuiltinType::Dependent:
193     ID = PREDEF_TYPE_DEPENDENT_ID;
194     break;
195   case BuiltinType::UnknownAny:
196     ID = PREDEF_TYPE_UNKNOWN_ANY;
197     break;
198   case BuiltinType::ARCUnbridgedCast:
199     ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;
200     break;
201   case BuiltinType::ObjCId:
202     ID = PREDEF_TYPE_OBJC_ID;
203     break;
204   case BuiltinType::ObjCClass:
205     ID = PREDEF_TYPE_OBJC_CLASS;
206     break;
207   case BuiltinType::ObjCSel:
208     ID = PREDEF_TYPE_OBJC_SEL;
209     break;
210 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
211   case BuiltinType::Id: \
212     ID = PREDEF_TYPE_##Id##_ID; \
213     break;
214 #include "clang/Basic/OpenCLImageTypes.def"
215 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
216   case BuiltinType::Id: \
217     ID = PREDEF_TYPE_##Id##_ID; \
218     break;
219 #include "clang/Basic/OpenCLExtensionTypes.def"
220   case BuiltinType::OCLSampler:
221     ID = PREDEF_TYPE_SAMPLER_ID;
222     break;
223   case BuiltinType::OCLEvent:
224     ID = PREDEF_TYPE_EVENT_ID;
225     break;
226   case BuiltinType::OCLClkEvent:
227     ID = PREDEF_TYPE_CLK_EVENT_ID;
228     break;
229   case BuiltinType::OCLQueue:
230     ID = PREDEF_TYPE_QUEUE_ID;
231     break;
232   case BuiltinType::OCLReserveID:
233     ID = PREDEF_TYPE_RESERVE_ID_ID;
234     break;
235 #define SVE_TYPE(Name, Id, SingletonId) \
236   case BuiltinType::Id: \
237     ID = PREDEF_TYPE_##Id##_ID; \
238     break;
239 #include "clang/Basic/AArch64SVEACLETypes.def"
240 #define PPC_VECTOR_TYPE(Name, Id, Size) \
241   case BuiltinType::Id: \
242     ID = PREDEF_TYPE_##Id##_ID; \
243     break;
244 #include "clang/Basic/PPCTypes.def"
245   case BuiltinType::BuiltinFn:
246     ID = PREDEF_TYPE_BUILTIN_FN;
247     break;
248   case BuiltinType::IncompleteMatrixIdx:
249     ID = PREDEF_TYPE_INCOMPLETE_MATRIX_IDX;
250     break;
251   case BuiltinType::OMPArraySection:
252     ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
253     break;
254   case BuiltinType::OMPArrayShaping:
255     ID = PREDEF_TYPE_OMP_ARRAY_SHAPING;
256     break;
257   case BuiltinType::OMPIterator:
258     ID = PREDEF_TYPE_OMP_ITERATOR;
259     break;
260   case BuiltinType::BFloat16:
261     ID = PREDEF_TYPE_BFLOAT16_ID;
262     break;
263   }
264 
265   return TypeIdx(ID);
266 }
267 
268 unsigned serialization::ComputeHash(Selector Sel) {
269   unsigned N = Sel.getNumArgs();
270   if (N == 0)
271     ++N;
272   unsigned R = 5381;
273   for (unsigned I = 0; I != N; ++I)
274     if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
275       R = llvm::djbHash(II->getName(), R);
276   return R;
277 }
278 
279 const DeclContext *
280 serialization::getDefinitiveDeclContext(const DeclContext *DC) {
281   switch (DC->getDeclKind()) {
282   // These entities may have multiple definitions.
283   case Decl::TranslationUnit:
284   case Decl::ExternCContext:
285   case Decl::Namespace:
286   case Decl::LinkageSpec:
287   case Decl::Export:
288     return nullptr;
289 
290   // C/C++ tag types can only be defined in one place.
291   case Decl::Enum:
292   case Decl::Record:
293     if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
294       return Def;
295     return nullptr;
296 
297   // FIXME: These can be defined in one place... except special member
298   // functions and out-of-line definitions.
299   case Decl::CXXRecord:
300   case Decl::ClassTemplateSpecialization:
301   case Decl::ClassTemplatePartialSpecialization:
302     return nullptr;
303 
304   // Each function, method, and block declaration is its own DeclContext.
305   case Decl::Function:
306   case Decl::CXXMethod:
307   case Decl::CXXConstructor:
308   case Decl::CXXDestructor:
309   case Decl::CXXConversion:
310   case Decl::ObjCMethod:
311   case Decl::Block:
312   case Decl::Captured:
313     // Objective C categories, category implementations, and class
314     // implementations can only be defined in one place.
315   case Decl::ObjCCategory:
316   case Decl::ObjCCategoryImpl:
317   case Decl::ObjCImplementation:
318     return DC;
319 
320   case Decl::ObjCProtocol:
321     if (const ObjCProtocolDecl *Def
322           = cast<ObjCProtocolDecl>(DC)->getDefinition())
323       return Def;
324     return nullptr;
325 
326   // FIXME: These are defined in one place, but properties in class extensions
327   // end up being back-patched into the main interface. See
328   // Sema::HandlePropertyInClassExtension for the offending code.
329   case Decl::ObjCInterface:
330     return nullptr;
331 
332   default:
333     llvm_unreachable("Unhandled DeclContext in AST reader");
334   }
335 
336   llvm_unreachable("Unhandled decl kind");
337 }
338 
339 bool serialization::isRedeclarableDeclKind(unsigned Kind) {
340   switch (static_cast<Decl::Kind>(Kind)) {
341   case Decl::TranslationUnit:
342   case Decl::ExternCContext:
343     // Special case of a "merged" declaration.
344     return true;
345 
346   case Decl::Namespace:
347   case Decl::NamespaceAlias:
348   case Decl::Typedef:
349   case Decl::TypeAlias:
350   case Decl::Enum:
351   case Decl::Record:
352   case Decl::CXXRecord:
353   case Decl::ClassTemplateSpecialization:
354   case Decl::ClassTemplatePartialSpecialization:
355   case Decl::VarTemplateSpecialization:
356   case Decl::VarTemplatePartialSpecialization:
357   case Decl::Function:
358   case Decl::CXXDeductionGuide:
359   case Decl::CXXMethod:
360   case Decl::CXXConstructor:
361   case Decl::CXXDestructor:
362   case Decl::CXXConversion:
363   case Decl::UsingShadow:
364   case Decl::ConstructorUsingShadow:
365   case Decl::Var:
366   case Decl::FunctionTemplate:
367   case Decl::ClassTemplate:
368   case Decl::VarTemplate:
369   case Decl::TypeAliasTemplate:
370   case Decl::ObjCProtocol:
371   case Decl::ObjCInterface:
372   case Decl::Empty:
373     return true;
374 
375   // Never redeclarable.
376   case Decl::UsingDirective:
377   case Decl::Label:
378   case Decl::UnresolvedUsingTypename:
379   case Decl::TemplateTypeParm:
380   case Decl::EnumConstant:
381   case Decl::UnresolvedUsingValue:
382   case Decl::IndirectField:
383   case Decl::Field:
384   case Decl::MSProperty:
385   case Decl::MSGuid:
386   case Decl::TemplateParamObject:
387   case Decl::ObjCIvar:
388   case Decl::ObjCAtDefsField:
389   case Decl::NonTypeTemplateParm:
390   case Decl::TemplateTemplateParm:
391   case Decl::Using:
392   case Decl::UsingPack:
393   case Decl::ObjCMethod:
394   case Decl::ObjCCategory:
395   case Decl::ObjCCategoryImpl:
396   case Decl::ObjCImplementation:
397   case Decl::ObjCProperty:
398   case Decl::ObjCCompatibleAlias:
399   case Decl::LinkageSpec:
400   case Decl::Export:
401   case Decl::ObjCPropertyImpl:
402   case Decl::PragmaComment:
403   case Decl::PragmaDetectMismatch:
404   case Decl::FileScopeAsm:
405   case Decl::AccessSpec:
406   case Decl::Friend:
407   case Decl::FriendTemplate:
408   case Decl::StaticAssert:
409   case Decl::Block:
410   case Decl::Captured:
411   case Decl::ClassScopeFunctionSpecialization:
412   case Decl::Import:
413   case Decl::OMPThreadPrivate:
414   case Decl::OMPAllocate:
415   case Decl::OMPRequires:
416   case Decl::OMPCapturedExpr:
417   case Decl::OMPDeclareReduction:
418   case Decl::OMPDeclareMapper:
419   case Decl::BuiltinTemplate:
420   case Decl::Decomposition:
421   case Decl::Binding:
422   case Decl::Concept:
423   case Decl::LifetimeExtendedTemporary:
424   case Decl::RequiresExprBody:
425     return false;
426 
427   // These indirectly derive from Redeclarable<T> but are not actually
428   // redeclarable.
429   case Decl::ImplicitParam:
430   case Decl::ParmVar:
431   case Decl::ObjCTypeParam:
432     return false;
433   }
434 
435   llvm_unreachable("Unhandled declaration kind");
436 }
437 
438 bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
439   // Friend declarations in dependent contexts aren't anonymous in the usual
440   // sense, but they cannot be found by name lookup in their semantic context
441   // (or indeed in any context), so we treat them as anonymous.
442   //
443   // This doesn't apply to friend tag decls; Sema makes those available to name
444   // lookup in the surrounding context.
445   if (D->getFriendObjectKind() &&
446       D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
447     // For function templates and class templates, the template is numbered and
448     // not its pattern.
449     if (auto *FD = dyn_cast<FunctionDecl>(D))
450       return !FD->getDescribedFunctionTemplate();
451     if (auto *RD = dyn_cast<CXXRecordDecl>(D))
452       return !RD->getDescribedClassTemplate();
453     return true;
454   }
455 
456   // At block scope, we number everything that we need to deduplicate, since we
457   // can't just use name matching to keep things lined up.
458   // FIXME: This is only necessary for an inline function or a template or
459   // similar.
460   if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
461     if (auto *VD = dyn_cast<VarDecl>(D))
462       return VD->isStaticLocal();
463     // FIXME: What about CapturedDecls (and declarations nested within them)?
464     return isa<TagDecl>(D) || isa<BlockDecl>(D);
465   }
466 
467   // Otherwise, we only care about anonymous class members / block-scope decls.
468   // FIXME: We need to handle lambdas and blocks within inline / templated
469   // variables too.
470   if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
471     return false;
472   return isa<TagDecl>(D) || isa<FieldDecl>(D);
473 }
474