clang  10.0.0svn
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 
20 using 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:
70  ID = PREDEF_TYPE_INT_ID;
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::NullPtr:
173  break;
174  case BuiltinType::Char8:
176  break;
177  case BuiltinType::Char16:
179  break;
180  case BuiltinType::Char32:
182  break;
183  case BuiltinType::Overload:
185  break;
186  case BuiltinType::BoundMember:
188  break;
189  case BuiltinType::PseudoObject:
191  break;
192  case BuiltinType::Dependent:
194  break;
195  case BuiltinType::UnknownAny:
197  break;
198  case BuiltinType::ARCUnbridgedCast:
200  break;
201  case BuiltinType::ObjCId:
202  ID = PREDEF_TYPE_OBJC_ID;
203  break;
204  case BuiltinType::ObjCClass:
206  break;
207  case BuiltinType::ObjCSel:
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:
222  break;
223  case BuiltinType::OCLEvent:
225  break;
226  case BuiltinType::OCLClkEvent:
228  break;
229  case BuiltinType::OCLQueue:
231  break;
232  case BuiltinType::OCLReserveID:
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  case BuiltinType::BuiltinFn:
242  break;
243  case BuiltinType::OMPArraySection:
245  break;
246  }
247 
248  return TypeIdx(ID);
249 }
250 
252  unsigned N = Sel.getNumArgs();
253  if (N == 0)
254  ++N;
255  unsigned R = 5381;
256  for (unsigned I = 0; I != N; ++I)
257  if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
258  R = llvm::djbHash(II->getName(), R);
259  return R;
260 }
261 
262 const DeclContext *
264  switch (DC->getDeclKind()) {
265  // These entities may have multiple definitions.
266  case Decl::TranslationUnit:
267  case Decl::ExternCContext:
268  case Decl::Namespace:
269  case Decl::LinkageSpec:
270  case Decl::Export:
271  return nullptr;
272 
273  // C/C++ tag types can only be defined in one place.
274  case Decl::Enum:
275  case Decl::Record:
276  if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
277  return Def;
278  return nullptr;
279 
280  // FIXME: These can be defined in one place... except special member
281  // functions and out-of-line definitions.
282  case Decl::CXXRecord:
283  case Decl::ClassTemplateSpecialization:
284  case Decl::ClassTemplatePartialSpecialization:
285  return nullptr;
286 
287  // Each function, method, and block declaration is its own DeclContext.
288  case Decl::Function:
289  case Decl::CXXMethod:
290  case Decl::CXXConstructor:
291  case Decl::CXXDestructor:
292  case Decl::CXXConversion:
293  case Decl::ObjCMethod:
294  case Decl::Block:
295  case Decl::Captured:
296  // Objective C categories, category implementations, and class
297  // implementations can only be defined in one place.
298  case Decl::ObjCCategory:
299  case Decl::ObjCCategoryImpl:
300  case Decl::ObjCImplementation:
301  return DC;
302 
303  case Decl::ObjCProtocol:
304  if (const ObjCProtocolDecl *Def
305  = cast<ObjCProtocolDecl>(DC)->getDefinition())
306  return Def;
307  return nullptr;
308 
309  // FIXME: These are defined in one place, but properties in class extensions
310  // end up being back-patched into the main interface. See
311  // Sema::HandlePropertyInClassExtension for the offending code.
312  case Decl::ObjCInterface:
313  return nullptr;
314 
315  default:
316  llvm_unreachable("Unhandled DeclContext in AST reader");
317  }
318 
319  llvm_unreachable("Unhandled decl kind");
320 }
321 
323  switch (static_cast<Decl::Kind>(Kind)) {
324  case Decl::TranslationUnit:
325  case Decl::ExternCContext:
326  // Special case of a "merged" declaration.
327  return true;
328 
329  case Decl::Namespace:
330  case Decl::NamespaceAlias:
331  case Decl::Typedef:
332  case Decl::TypeAlias:
333  case Decl::Enum:
334  case Decl::Record:
335  case Decl::CXXRecord:
336  case Decl::ClassTemplateSpecialization:
337  case Decl::ClassTemplatePartialSpecialization:
338  case Decl::VarTemplateSpecialization:
339  case Decl::VarTemplatePartialSpecialization:
340  case Decl::Function:
341  case Decl::CXXDeductionGuide:
342  case Decl::CXXMethod:
343  case Decl::CXXConstructor:
344  case Decl::CXXDestructor:
345  case Decl::CXXConversion:
346  case Decl::UsingShadow:
347  case Decl::ConstructorUsingShadow:
348  case Decl::Var:
349  case Decl::FunctionTemplate:
350  case Decl::ClassTemplate:
351  case Decl::VarTemplate:
352  case Decl::TypeAliasTemplate:
353  case Decl::ObjCProtocol:
354  case Decl::ObjCInterface:
355  case Decl::Empty:
356  return true;
357 
358  // Never redeclarable.
359  case Decl::UsingDirective:
360  case Decl::Label:
361  case Decl::UnresolvedUsingTypename:
362  case Decl::TemplateTypeParm:
363  case Decl::EnumConstant:
364  case Decl::UnresolvedUsingValue:
365  case Decl::IndirectField:
366  case Decl::Field:
367  case Decl::MSProperty:
368  case Decl::ObjCIvar:
369  case Decl::ObjCAtDefsField:
370  case Decl::NonTypeTemplateParm:
371  case Decl::TemplateTemplateParm:
372  case Decl::Using:
373  case Decl::UsingPack:
374  case Decl::ObjCMethod:
375  case Decl::ObjCCategory:
376  case Decl::ObjCCategoryImpl:
377  case Decl::ObjCImplementation:
378  case Decl::ObjCProperty:
379  case Decl::ObjCCompatibleAlias:
380  case Decl::LinkageSpec:
381  case Decl::Export:
382  case Decl::ObjCPropertyImpl:
383  case Decl::PragmaComment:
384  case Decl::PragmaDetectMismatch:
385  case Decl::FileScopeAsm:
386  case Decl::AccessSpec:
387  case Decl::Friend:
388  case Decl::FriendTemplate:
389  case Decl::StaticAssert:
390  case Decl::Block:
391  case Decl::Captured:
392  case Decl::ClassScopeFunctionSpecialization:
393  case Decl::Import:
394  case Decl::OMPThreadPrivate:
395  case Decl::OMPAllocate:
396  case Decl::OMPRequires:
397  case Decl::OMPCapturedExpr:
398  case Decl::OMPDeclareReduction:
399  case Decl::OMPDeclareMapper:
400  case Decl::BuiltinTemplate:
401  case Decl::Decomposition:
402  case Decl::Binding:
403  case Decl::Concept:
404  return false;
405 
406  // These indirectly derive from Redeclarable<T> but are not actually
407  // redeclarable.
408  case Decl::ImplicitParam:
409  case Decl::ParmVar:
410  case Decl::ObjCTypeParam:
411  return false;
412  }
413 
414  llvm_unreachable("Unhandled declaration kind");
415 }
416 
418  // Friend declarations in dependent contexts aren't anonymous in the usual
419  // sense, but they cannot be found by name lookup in their semantic context
420  // (or indeed in any context), so we treat them as anonymous.
421  //
422  // This doesn't apply to friend tag decls; Sema makes those available to name
423  // lookup in the surrounding context.
424  if (D->getFriendObjectKind() &&
425  D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
426  // For function templates and class templates, the template is numbered and
427  // not its pattern.
428  if (auto *FD = dyn_cast<FunctionDecl>(D))
429  return !FD->getDescribedFunctionTemplate();
430  if (auto *RD = dyn_cast<CXXRecordDecl>(D))
431  return !RD->getDescribedClassTemplate();
432  return true;
433  }
434 
435  // At block scope, we number everything that we need to deduplicate, since we
436  // can't just use name matching to keep things lined up.
437  // FIXME: This is only necessary for an inline function or a template or
438  // similar.
440  if (auto *VD = dyn_cast<VarDecl>(D))
441  return VD->isStaticLocal();
442  // FIXME: What about CapturedDecls (and declarations nested within them)?
443  return isa<TagDecl>(D) || isa<BlockDecl>(D);
444  }
445 
446  // Otherwise, we only care about anonymous class members / block-scope decls.
447  // FIXME: We need to handle lambdas and blocks within inline / templated
448  // variables too.
449  if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
450  return false;
451  return isa<TagDecl>(D) || isa<FieldDecl>(D);
452 }
The ObjC &#39;SEL&#39; type.
Definition: ASTBitCodes.h:891
The &#39;unsigned _Accum&#39; type.
Definition: ASTBitCodes.h:957
The &#39;unsigned short _Fract&#39; type.
Definition: ASTBitCodes.h:972
Smart pointer class that efficiently represents Objective-C method names.
bool isRedeclarableDeclKind(unsigned Kind)
Determine whether the given declaration kind is redeclarable.
Definition: ASTCommon.cpp:322
TagDecl * getDefinition() const
Returns the TagDecl that actually defines this struct/union/class/enum.
Definition: Decl.cpp:4095
The &#39;_Float16&#39; type.
Definition: ASTBitCodes.h:939
The &#39;unsigned int&#39; type.
Definition: ASTBitCodes.h:825
The (signed) &#39;long long&#39; type.
Definition: ASTBitCodes.h:852
The &#39;bool&#39; or &#39;_Bool&#39; type.
Definition: ASTBitCodes.h:813
Kind getKind() const
Definition: Type.h:2466
The &#39;unknown any&#39; placeholder type.
Definition: ASTBitCodes.h:894
The placeholder type for builtin functions.
Definition: ASTBitCodes.h:915
constexpr XRayInstrMask Function
Definition: XRayInstr.h:38
The &#39;short _Fract&#39; type.
Definition: ASTBitCodes.h:963
ARC&#39;s unbridged-cast placeholder type.
Definition: ASTBitCodes.h:909
The &#39;__int128_t&#39; type.
Definition: ASTBitCodes.h:873
The C++ &#39;char32_t&#39; type.
Definition: ASTBitCodes.h:882
const DeclContext * getDefinitiveDeclContext(const DeclContext *DC)
Retrieve the "definitive" declaration that provides all of the visible entries for the given declarat...
Definition: ASTCommon.cpp:263
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:297
One of these records is kept for each identifier that is lexed.
The &#39;char&#39; type, when it is signed.
Definition: ASTBitCodes.h:834
The &#39;unsigned long&#39; type.
Definition: ASTBitCodes.h:828
The &#39;_Sat unsigned _Accum&#39; type.
Definition: ASTBitCodes.h:993
The &#39;unsigned long _Fract&#39; type.
Definition: ASTBitCodes.h:978
The &#39;long _Fract&#39; type.
Definition: ASTBitCodes.h:969
The &#39;short _Accum&#39; type.
Definition: ASTBitCodes.h:945
The &#39;long _Accum&#39; type.
Definition: ASTBitCodes.h:951
The &#39;unsigned short _Accum&#39; type.
Definition: ASTBitCodes.h:954
ObjCProtocolDecl * getDefinition()
Retrieve the definition of this protocol, if any.
Definition: DeclObjC.h:2209
The &#39;unsigned short&#39; type.
Definition: ASTBitCodes.h:822
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:2063
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC)...
Definition: DeclBase.h:828
The &#39;_Sat short _Fract&#39; type.
Definition: ASTBitCodes.h:999
The &#39;unsigned long _Accum&#39; type.
Definition: ASTBitCodes.h:960
The type of &#39;nullptr&#39;.
Definition: ASTBitCodes.h:876
The &#39;_Sat unsigned long _Fract&#39; type.
Definition: ASTBitCodes.h:1014
The placeholder type for OpenMP array section.
Definition: ASTBitCodes.h:933
The &#39;__uint128_t&#39; type.
Definition: ASTBitCodes.h:870
The &#39;char&#39; type, when it is unsigned.
Definition: ASTBitCodes.h:816
std::string Label
The &#39;_Sat long _Fract&#39; type.
Definition: ASTBitCodes.h:1005
The placeholder type for overloaded function sets.
Definition: ASTBitCodes.h:864
The OpenCL &#39;half&#39; / ARM NEON __fp16 type.
Definition: ASTBitCodes.h:906
The &#39;long double&#39; type.
Definition: ASTBitCodes.h:861
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
bool isFunctionOrMethod() const
Definition: DeclBase.h:1831
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
Definition: DeclBase.cpp:1089
unsigned getNumArgs() const
FriendObjectKind getFriendObjectKind() const
Determines whether this declaration is the object of a friend declaration and, if so...
Definition: DeclBase.h:1111
The &#39;_Sat unsigned long _Accum&#39; type.
Definition: ASTBitCodes.h:996
The placeholder type for bound member functions.
Definition: ASTBitCodes.h:897
The &#39;_Sat short _Accum&#39; type.
Definition: ASTBitCodes.h:981
The &#39;unsigned long long&#39; type.
Definition: ASTBitCodes.h:831
Decl::Kind getDeclKind() const
Definition: DeclBase.h:1772
The C++ &#39;char8_t&#39; type.
Definition: ASTBitCodes.h:942
Kind
IdentifierInfo * getIdentifierInfoForSlot(unsigned argIndex) const
Retrieve the identifier at a given position in the selector.
The &#39;_Sat unsigned _Fract&#39; type.
Definition: ASTBitCodes.h:1011
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3133
The &#39;unsigned char&#39; type.
Definition: ASTBitCodes.h:819
The (signed) &#39;long&#39; type.
Definition: ASTBitCodes.h:849
The (signed) &#39;short&#39; type.
Definition: ASTBitCodes.h:843
The &#39;_Sat _Accum&#39; type.
Definition: ASTBitCodes.h:984
TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT)
Definition: ASTCommon.cpp:26
The &#39;signed char&#39; type.
Definition: ASTBitCodes.h:837
unsigned ComputeHash(Selector Sel)
Definition: ASTCommon.cpp:251
The ObjC &#39;id&#39; type.
Definition: ASTBitCodes.h:885
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1271
The &#39;_Sat unsigned short _Accum&#39; type.
Definition: ASTBitCodes.h:990
The &#39;_Sat long _Accum&#39; type.
Definition: ASTBitCodes.h:987
The placeholder type for dependent types.
Definition: ASTBitCodes.h:867
The &#39;unsigned _Fract&#39; type.
Definition: ASTBitCodes.h:975
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
The C++ &#39;char16_t&#39; type.
Definition: ASTBitCodes.h:879
bool needsAnonymousDeclarationNumber(const NamedDecl *D)
Determine whether the given declaration needs an anonymous declaration number.
Definition: ASTCommon.cpp:417
This class is used for builtin types like &#39;int&#39;.
Definition: Type.h:2436
The (signed) &#39;int&#39; type.
Definition: ASTBitCodes.h:846
The C++ &#39;wchar_t&#39; type.
Definition: ASTBitCodes.h:840
The ObjC &#39;Class&#39; type.
Definition: ASTBitCodes.h:888
This represents a decl that may have a name.
Definition: Decl.h:248
The &#39;_Sat unsigned short _Fract&#39; type.
Definition: ASTBitCodes.h:1008
The &#39;_Sat _Fract&#39; type.
Definition: ASTBitCodes.h:1002
A type index; the type ID with the qualifier bits removed.
Definition: ASTBitCodes.h:88
The pseudo-object placeholder type.
Definition: ASTBitCodes.h:912
The &#39;__float128&#39; type.
Definition: ASTBitCodes.h:936