clang  8.0.0svn
ASTCommon.cpp
Go to the documentation of this file.
1 //===--- 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"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/DeclObjC.h"
19 #include "llvm/Support/DJB.h"
20 
21 using namespace clang;
22 
23 // Give ASTDeserializationListener's VTable a home.
25 
28  unsigned ID = 0;
29  switch (BT->getKind()) {
30  case BuiltinType::Void:
32  break;
33  case BuiltinType::Bool:
35  break;
36  case BuiltinType::Char_U:
38  break;
39  case BuiltinType::UChar:
41  break;
42  case BuiltinType::UShort:
44  break;
45  case BuiltinType::UInt:
47  break;
48  case BuiltinType::ULong:
50  break;
51  case BuiltinType::ULongLong:
53  break;
54  case BuiltinType::UInt128:
56  break;
57  case BuiltinType::Char_S:
59  break;
60  case BuiltinType::SChar:
62  break;
63  case BuiltinType::WChar_S:
64  case BuiltinType::WChar_U:
66  break;
67  case BuiltinType::Short:
69  break;
70  case BuiltinType::Int:
71  ID = PREDEF_TYPE_INT_ID;
72  break;
73  case BuiltinType::Long:
75  break;
76  case BuiltinType::LongLong:
78  break;
79  case BuiltinType::Int128:
81  break;
82  case BuiltinType::Half:
84  break;
85  case BuiltinType::Float:
87  break;
88  case BuiltinType::Double:
90  break;
91  case BuiltinType::LongDouble:
93  break;
94  case BuiltinType::ShortAccum:
96  break;
97  case BuiltinType::Accum:
99  break;
100  case BuiltinType::LongAccum:
102  break;
103  case BuiltinType::UShortAccum:
105  break;
106  case BuiltinType::UAccum:
108  break;
109  case BuiltinType::ULongAccum:
111  break;
112  case BuiltinType::ShortFract:
114  break;
115  case BuiltinType::Fract:
117  break;
118  case BuiltinType::LongFract:
120  break;
121  case BuiltinType::UShortFract:
123  break;
124  case BuiltinType::UFract:
126  break;
127  case BuiltinType::ULongFract:
129  break;
130  case BuiltinType::SatShortAccum:
132  break;
133  case BuiltinType::SatAccum:
135  break;
136  case BuiltinType::SatLongAccum:
138  break;
139  case BuiltinType::SatUShortAccum:
141  break;
142  case BuiltinType::SatUAccum:
144  break;
145  case BuiltinType::SatULongAccum:
147  break;
148  case BuiltinType::SatShortFract:
150  break;
151  case BuiltinType::SatFract:
153  break;
154  case BuiltinType::SatLongFract:
156  break;
157  case BuiltinType::SatUShortFract:
159  break;
160  case BuiltinType::SatUFract:
162  break;
163  case BuiltinType::SatULongFract:
165  break;
166  case BuiltinType::Float16:
168  break;
169  case BuiltinType::Float128:
171  break;
172  case BuiltinType::NullPtr:
174  break;
175  case BuiltinType::Char8:
177  break;
178  case BuiltinType::Char16:
180  break;
181  case BuiltinType::Char32:
183  break;
184  case BuiltinType::Overload:
186  break;
187  case BuiltinType::BoundMember:
189  break;
190  case BuiltinType::PseudoObject:
192  break;
193  case BuiltinType::Dependent:
195  break;
196  case BuiltinType::UnknownAny:
198  break;
199  case BuiltinType::ARCUnbridgedCast:
201  break;
202  case BuiltinType::ObjCId:
203  ID = PREDEF_TYPE_OBJC_ID;
204  break;
205  case BuiltinType::ObjCClass:
207  break;
208  case BuiltinType::ObjCSel:
210  break;
211 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
212  case BuiltinType::Id: \
213  ID = PREDEF_TYPE_##Id##_ID; \
214  break;
215 #include "clang/Basic/OpenCLImageTypes.def"
216  case BuiltinType::OCLSampler:
218  break;
219  case BuiltinType::OCLEvent:
221  break;
222  case BuiltinType::OCLClkEvent:
224  break;
225  case BuiltinType::OCLQueue:
227  break;
228  case BuiltinType::OCLReserveID:
230  break;
231  case BuiltinType::BuiltinFn:
233  break;
234  case BuiltinType::OMPArraySection:
236  break;
237  }
238 
239  return TypeIdx(ID);
240 }
241 
243  unsigned N = Sel.getNumArgs();
244  if (N == 0)
245  ++N;
246  unsigned R = 5381;
247  for (unsigned I = 0; I != N; ++I)
248  if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
249  R = llvm::djbHash(II->getName(), R);
250  return R;
251 }
252 
253 const DeclContext *
255  switch (DC->getDeclKind()) {
256  // These entities may have multiple definitions.
257  case Decl::TranslationUnit:
258  case Decl::ExternCContext:
259  case Decl::Namespace:
260  case Decl::LinkageSpec:
261  case Decl::Export:
262  return nullptr;
263 
264  // C/C++ tag types can only be defined in one place.
265  case Decl::Enum:
266  case Decl::Record:
267  if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
268  return Def;
269  return nullptr;
270 
271  // FIXME: These can be defined in one place... except special member
272  // functions and out-of-line definitions.
273  case Decl::CXXRecord:
274  case Decl::ClassTemplateSpecialization:
275  case Decl::ClassTemplatePartialSpecialization:
276  return nullptr;
277 
278  // Each function, method, and block declaration is its own DeclContext.
279  case Decl::Function:
280  case Decl::CXXMethod:
281  case Decl::CXXConstructor:
282  case Decl::CXXDestructor:
283  case Decl::CXXConversion:
284  case Decl::ObjCMethod:
285  case Decl::Block:
286  case Decl::Captured:
287  // Objective C categories, category implementations, and class
288  // implementations can only be defined in one place.
289  case Decl::ObjCCategory:
290  case Decl::ObjCCategoryImpl:
291  case Decl::ObjCImplementation:
292  return DC;
293 
294  case Decl::ObjCProtocol:
295  if (const ObjCProtocolDecl *Def
296  = cast<ObjCProtocolDecl>(DC)->getDefinition())
297  return Def;
298  return nullptr;
299 
300  // FIXME: These are defined in one place, but properties in class extensions
301  // end up being back-patched into the main interface. See
302  // Sema::HandlePropertyInClassExtension for the offending code.
303  case Decl::ObjCInterface:
304  return nullptr;
305 
306  default:
307  llvm_unreachable("Unhandled DeclContext in AST reader");
308  }
309 
310  llvm_unreachable("Unhandled decl kind");
311 }
312 
314  switch (static_cast<Decl::Kind>(Kind)) {
315  case Decl::TranslationUnit:
316  case Decl::ExternCContext:
317  // Special case of a "merged" declaration.
318  return true;
319 
320  case Decl::Namespace:
321  case Decl::NamespaceAlias:
322  case Decl::Typedef:
323  case Decl::TypeAlias:
324  case Decl::Enum:
325  case Decl::Record:
326  case Decl::CXXRecord:
327  case Decl::ClassTemplateSpecialization:
328  case Decl::ClassTemplatePartialSpecialization:
329  case Decl::VarTemplateSpecialization:
330  case Decl::VarTemplatePartialSpecialization:
331  case Decl::Function:
332  case Decl::CXXDeductionGuide:
333  case Decl::CXXMethod:
334  case Decl::CXXConstructor:
335  case Decl::CXXDestructor:
336  case Decl::CXXConversion:
337  case Decl::UsingShadow:
338  case Decl::ConstructorUsingShadow:
339  case Decl::Var:
340  case Decl::FunctionTemplate:
341  case Decl::ClassTemplate:
342  case Decl::VarTemplate:
343  case Decl::TypeAliasTemplate:
344  case Decl::ObjCProtocol:
345  case Decl::ObjCInterface:
346  case Decl::Empty:
347  return true;
348 
349  // Never redeclarable.
350  case Decl::UsingDirective:
351  case Decl::Label:
352  case Decl::UnresolvedUsingTypename:
353  case Decl::TemplateTypeParm:
354  case Decl::EnumConstant:
355  case Decl::UnresolvedUsingValue:
356  case Decl::IndirectField:
357  case Decl::Field:
358  case Decl::MSProperty:
359  case Decl::ObjCIvar:
360  case Decl::ObjCAtDefsField:
361  case Decl::NonTypeTemplateParm:
362  case Decl::TemplateTemplateParm:
363  case Decl::Using:
364  case Decl::UsingPack:
365  case Decl::ObjCMethod:
366  case Decl::ObjCCategory:
367  case Decl::ObjCCategoryImpl:
368  case Decl::ObjCImplementation:
369  case Decl::ObjCProperty:
370  case Decl::ObjCCompatibleAlias:
371  case Decl::LinkageSpec:
372  case Decl::Export:
373  case Decl::ObjCPropertyImpl:
374  case Decl::PragmaComment:
375  case Decl::PragmaDetectMismatch:
376  case Decl::FileScopeAsm:
377  case Decl::AccessSpec:
378  case Decl::Friend:
379  case Decl::FriendTemplate:
380  case Decl::StaticAssert:
381  case Decl::Block:
382  case Decl::Captured:
383  case Decl::ClassScopeFunctionSpecialization:
384  case Decl::Import:
385  case Decl::OMPThreadPrivate:
386  case Decl::OMPCapturedExpr:
387  case Decl::OMPDeclareReduction:
388  case Decl::BuiltinTemplate:
389  case Decl::Decomposition:
390  case Decl::Binding:
391  return false;
392 
393  // These indirectly derive from Redeclarable<T> but are not actually
394  // redeclarable.
395  case Decl::ImplicitParam:
396  case Decl::ParmVar:
397  case Decl::ObjCTypeParam:
398  return false;
399  }
400 
401  llvm_unreachable("Unhandled declaration kind");
402 }
403 
405  // Friend declarations in dependent contexts aren't anonymous in the usual
406  // sense, but they cannot be found by name lookup in their semantic context
407  // (or indeed in any context), so we treat them as anonymous.
408  //
409  // This doesn't apply to friend tag decls; Sema makes those available to name
410  // lookup in the surrounding context.
411  if (D->getFriendObjectKind() &&
412  D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
413  // For function templates and class templates, the template is numbered and
414  // not its pattern.
415  if (auto *FD = dyn_cast<FunctionDecl>(D))
416  return !FD->getDescribedFunctionTemplate();
417  if (auto *RD = dyn_cast<CXXRecordDecl>(D))
418  return !RD->getDescribedClassTemplate();
419  return true;
420  }
421 
422  // At block scope, we number everything that we need to deduplicate, since we
423  // can't just use name matching to keep things lined up.
424  // FIXME: This is only necessary for an inline function or a template or
425  // similar.
427  if (auto *VD = dyn_cast<VarDecl>(D))
428  return VD->isStaticLocal();
429  // FIXME: What about CapturedDecls (and declarations nested within them)?
430  return isa<TagDecl>(D) || isa<BlockDecl>(D);
431  }
432 
433  // Otherwise, we only care about anonymous class members / block-scope decls.
434  // FIXME: We need to handle lambdas and blocks within inline / templated
435  // variables too.
436  if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
437  return false;
438  return isa<TagDecl>(D) || isa<FieldDecl>(D);
439 }
The ObjC &#39;SEL&#39; type.
Definition: ASTBitCodes.h:889
The &#39;unsigned _Accum&#39; type.
Definition: ASTBitCodes.h:955
The &#39;unsigned short _Fract&#39; type.
Definition: ASTBitCodes.h:970
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:313
TagDecl * getDefinition() const
Returns the TagDecl that actually defines this struct/union/class/enum.
Definition: Decl.cpp:3878
The &#39;_Float16&#39; type.
Definition: ASTBitCodes.h:937
The &#39;unsigned int&#39; type.
Definition: ASTBitCodes.h:823
The (signed) &#39;long long&#39; type.
Definition: ASTBitCodes.h:850
The &#39;bool&#39; or &#39;_Bool&#39; type.
Definition: ASTBitCodes.h:811
Kind getKind() const
Definition: Type.h:2392
The &#39;unknown any&#39; placeholder type.
Definition: ASTBitCodes.h:892
The placeholder type for builtin functions.
Definition: ASTBitCodes.h:913
constexpr XRayInstrMask Function
Definition: XRayInstr.h:39
The &#39;short _Fract&#39; type.
Definition: ASTBitCodes.h:961
ARC&#39;s unbridged-cast placeholder type.
Definition: ASTBitCodes.h:907
The &#39;__int128_t&#39; type.
Definition: ASTBitCodes.h:871
The C++ &#39;char32_t&#39; type.
Definition: ASTBitCodes.h:880
const DeclContext * getDefinitiveDeclContext(const DeclContext *DC)
Retrieve the "definitive" declaration that provides all of the visible entries for the given declarat...
Definition: ASTCommon.cpp:254
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:832
The &#39;unsigned long&#39; type.
Definition: ASTBitCodes.h:826
The &#39;_Sat unsigned _Accum&#39; type.
Definition: ASTBitCodes.h:991
The &#39;unsigned long _Fract&#39; type.
Definition: ASTBitCodes.h:976
The &#39;long _Fract&#39; type.
Definition: ASTBitCodes.h:967
The &#39;short _Accum&#39; type.
Definition: ASTBitCodes.h:943
The &#39;long _Accum&#39; type.
Definition: ASTBitCodes.h:949
The &#39;unsigned short _Accum&#39; type.
Definition: ASTBitCodes.h:952
ObjCProtocolDecl * getDefinition()
Retrieve the definition of this protocol, if any.
Definition: DeclObjC.h:2202
The &#39;unsigned short&#39; type.
Definition: ASTBitCodes.h:820
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:2056
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC)...
Definition: DeclBase.h:826
The &#39;_Sat short _Fract&#39; type.
Definition: ASTBitCodes.h:997
The &#39;unsigned long _Accum&#39; type.
Definition: ASTBitCodes.h:958
The type of &#39;nullptr&#39;.
Definition: ASTBitCodes.h:874
The &#39;_Sat unsigned long _Fract&#39; type.
Definition: ASTBitCodes.h:1012
The placeholder type for OpenMP array section.
Definition: ASTBitCodes.h:931
The &#39;__uint128_t&#39; type.
Definition: ASTBitCodes.h:868
The &#39;char&#39; type, when it is unsigned.
Definition: ASTBitCodes.h:814
std::string Label
The &#39;_Sat long _Fract&#39; type.
Definition: ASTBitCodes.h:1003
The placeholder type for overloaded function sets.
Definition: ASTBitCodes.h:862
The OpenCL &#39;half&#39; / ARM NEON __fp16 type.
Definition: ASTBitCodes.h:904
The &#39;long double&#39; type.
Definition: ASTBitCodes.h:859
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
bool isFunctionOrMethod() const
Definition: DeclBase.h:1812
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
Definition: DeclBase.cpp:1057
unsigned getNumArgs() const
FriendObjectKind getFriendObjectKind() const
Determines whether this declaration is the object of a friend declaration and, if so...
Definition: DeclBase.h:1108
The &#39;_Sat unsigned long _Accum&#39; type.
Definition: ASTBitCodes.h:994
The placeholder type for bound member functions.
Definition: ASTBitCodes.h:895
The &#39;_Sat short _Accum&#39; type.
Definition: ASTBitCodes.h:979
The &#39;unsigned long long&#39; type.
Definition: ASTBitCodes.h:829
Decl::Kind getDeclKind() const
Definition: DeclBase.h:1757
The C++ &#39;char8_t&#39; type.
Definition: ASTBitCodes.h:940
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:1009
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3041
The &#39;unsigned char&#39; type.
Definition: ASTBitCodes.h:817
The (signed) &#39;long&#39; type.
Definition: ASTBitCodes.h:847
The (signed) &#39;short&#39; type.
Definition: ASTBitCodes.h:841
The &#39;_Sat _Accum&#39; type.
Definition: ASTBitCodes.h:982
TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT)
Definition: ASTCommon.cpp:27
The &#39;signed char&#39; type.
Definition: ASTBitCodes.h:835
unsigned ComputeHash(Selector Sel)
Definition: ASTCommon.cpp:242
The ObjC &#39;id&#39; type.
Definition: ASTBitCodes.h:883
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:1267
The &#39;_Sat unsigned short _Accum&#39; type.
Definition: ASTBitCodes.h:988
The &#39;_Sat long _Accum&#39; type.
Definition: ASTBitCodes.h:985
The placeholder type for dependent types.
Definition: ASTBitCodes.h:865
The &#39;unsigned _Fract&#39; type.
Definition: ASTBitCodes.h:973
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:877
bool needsAnonymousDeclarationNumber(const NamedDecl *D)
Determine whether the given declaration needs an anonymous declaration number.
Definition: ASTCommon.cpp:404
This class is used for builtin types like &#39;int&#39;.
Definition: Type.h:2368
The (signed) &#39;int&#39; type.
Definition: ASTBitCodes.h:844
The C++ &#39;wchar_t&#39; type.
Definition: ASTBitCodes.h:838
The ObjC &#39;Class&#39; type.
Definition: ASTBitCodes.h:886
This represents a decl that may have a name.
Definition: Decl.h:248
The &#39;_Sat unsigned short _Fract&#39; type.
Definition: ASTBitCodes.h:1006
The &#39;_Sat _Fract&#39; type.
Definition: ASTBitCodes.h:1000
A type index; the type ID with the qualifier bits removed.
Definition: ASTBitCodes.h:89
The pseudo-object placeholder type.
Definition: ASTBitCodes.h:910
The &#39;__float128&#39; type.
Definition: ASTBitCodes.h:934