clang  7.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::Float16:
96  break;
97  case BuiltinType::Float128:
99  break;
100  case BuiltinType::NullPtr:
102  break;
103  case BuiltinType::Char8:
105  break;
106  case BuiltinType::Char16:
108  break;
109  case BuiltinType::Char32:
111  break;
112  case BuiltinType::Overload:
114  break;
115  case BuiltinType::BoundMember:
117  break;
118  case BuiltinType::PseudoObject:
120  break;
121  case BuiltinType::Dependent:
123  break;
124  case BuiltinType::UnknownAny:
126  break;
127  case BuiltinType::ARCUnbridgedCast:
129  break;
130  case BuiltinType::ObjCId:
131  ID = PREDEF_TYPE_OBJC_ID;
132  break;
133  case BuiltinType::ObjCClass:
135  break;
136  case BuiltinType::ObjCSel:
138  break;
139 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
140  case BuiltinType::Id: \
141  ID = PREDEF_TYPE_##Id##_ID; \
142  break;
143 #include "clang/Basic/OpenCLImageTypes.def"
144  case BuiltinType::OCLSampler:
146  break;
147  case BuiltinType::OCLEvent:
149  break;
150  case BuiltinType::OCLClkEvent:
152  break;
153  case BuiltinType::OCLQueue:
155  break;
156  case BuiltinType::OCLReserveID:
158  break;
159  case BuiltinType::BuiltinFn:
161  break;
162  case BuiltinType::OMPArraySection:
164  break;
165  }
166 
167  return TypeIdx(ID);
168 }
169 
171  unsigned N = Sel.getNumArgs();
172  if (N == 0)
173  ++N;
174  unsigned R = 5381;
175  for (unsigned I = 0; I != N; ++I)
176  if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
177  R = llvm::djbHash(II->getName(), R);
178  return R;
179 }
180 
181 const DeclContext *
183  switch (DC->getDeclKind()) {
184  // These entities may have multiple definitions.
185  case Decl::TranslationUnit:
186  case Decl::ExternCContext:
187  case Decl::Namespace:
188  case Decl::LinkageSpec:
189  case Decl::Export:
190  return nullptr;
191 
192  // C/C++ tag types can only be defined in one place.
193  case Decl::Enum:
194  case Decl::Record:
195  if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
196  return Def;
197  return nullptr;
198 
199  // FIXME: These can be defined in one place... except special member
200  // functions and out-of-line definitions.
201  case Decl::CXXRecord:
202  case Decl::ClassTemplateSpecialization:
203  case Decl::ClassTemplatePartialSpecialization:
204  return nullptr;
205 
206  // Each function, method, and block declaration is its own DeclContext.
207  case Decl::Function:
208  case Decl::CXXMethod:
209  case Decl::CXXConstructor:
210  case Decl::CXXDestructor:
211  case Decl::CXXConversion:
212  case Decl::ObjCMethod:
213  case Decl::Block:
214  case Decl::Captured:
215  // Objective C categories, category implementations, and class
216  // implementations can only be defined in one place.
217  case Decl::ObjCCategory:
218  case Decl::ObjCCategoryImpl:
219  case Decl::ObjCImplementation:
220  return DC;
221 
222  case Decl::ObjCProtocol:
223  if (const ObjCProtocolDecl *Def
224  = cast<ObjCProtocolDecl>(DC)->getDefinition())
225  return Def;
226  return nullptr;
227 
228  // FIXME: These are defined in one place, but properties in class extensions
229  // end up being back-patched into the main interface. See
230  // Sema::HandlePropertyInClassExtension for the offending code.
231  case Decl::ObjCInterface:
232  return nullptr;
233 
234  default:
235  llvm_unreachable("Unhandled DeclContext in AST reader");
236  }
237 
238  llvm_unreachable("Unhandled decl kind");
239 }
240 
242  switch (static_cast<Decl::Kind>(Kind)) {
243  case Decl::TranslationUnit:
244  case Decl::ExternCContext:
245  // Special case of a "merged" declaration.
246  return true;
247 
248  case Decl::Namespace:
249  case Decl::NamespaceAlias:
250  case Decl::Typedef:
251  case Decl::TypeAlias:
252  case Decl::Enum:
253  case Decl::Record:
254  case Decl::CXXRecord:
255  case Decl::ClassTemplateSpecialization:
256  case Decl::ClassTemplatePartialSpecialization:
257  case Decl::VarTemplateSpecialization:
258  case Decl::VarTemplatePartialSpecialization:
259  case Decl::Function:
260  case Decl::CXXDeductionGuide:
261  case Decl::CXXMethod:
262  case Decl::CXXConstructor:
263  case Decl::CXXDestructor:
264  case Decl::CXXConversion:
265  case Decl::UsingShadow:
266  case Decl::ConstructorUsingShadow:
267  case Decl::Var:
268  case Decl::FunctionTemplate:
269  case Decl::ClassTemplate:
270  case Decl::VarTemplate:
271  case Decl::TypeAliasTemplate:
272  case Decl::ObjCProtocol:
273  case Decl::ObjCInterface:
274  case Decl::Empty:
275  return true;
276 
277  // Never redeclarable.
278  case Decl::UsingDirective:
279  case Decl::Label:
280  case Decl::UnresolvedUsingTypename:
281  case Decl::TemplateTypeParm:
282  case Decl::EnumConstant:
283  case Decl::UnresolvedUsingValue:
284  case Decl::IndirectField:
285  case Decl::Field:
286  case Decl::MSProperty:
287  case Decl::ObjCIvar:
288  case Decl::ObjCAtDefsField:
289  case Decl::NonTypeTemplateParm:
290  case Decl::TemplateTemplateParm:
291  case Decl::Using:
292  case Decl::UsingPack:
293  case Decl::ObjCMethod:
294  case Decl::ObjCCategory:
295  case Decl::ObjCCategoryImpl:
296  case Decl::ObjCImplementation:
297  case Decl::ObjCProperty:
298  case Decl::ObjCCompatibleAlias:
299  case Decl::LinkageSpec:
300  case Decl::Export:
301  case Decl::ObjCPropertyImpl:
302  case Decl::PragmaComment:
303  case Decl::PragmaDetectMismatch:
304  case Decl::FileScopeAsm:
305  case Decl::AccessSpec:
306  case Decl::Friend:
307  case Decl::FriendTemplate:
308  case Decl::StaticAssert:
309  case Decl::Block:
310  case Decl::Captured:
311  case Decl::ClassScopeFunctionSpecialization:
312  case Decl::Import:
313  case Decl::OMPThreadPrivate:
314  case Decl::OMPCapturedExpr:
315  case Decl::OMPDeclareReduction:
316  case Decl::BuiltinTemplate:
317  case Decl::Decomposition:
318  case Decl::Binding:
319  return false;
320 
321  // These indirectly derive from Redeclarable<T> but are not actually
322  // redeclarable.
323  case Decl::ImplicitParam:
324  case Decl::ParmVar:
325  case Decl::ObjCTypeParam:
326  return false;
327  }
328 
329  llvm_unreachable("Unhandled declaration kind");
330 }
331 
333  // Friend declarations in dependent contexts aren't anonymous in the usual
334  // sense, but they cannot be found by name lookup in their semantic context
335  // (or indeed in any context), so we treat them as anonymous.
336  //
337  // This doesn't apply to friend tag decls; Sema makes those available to name
338  // lookup in the surrounding context.
339  if (D->getFriendObjectKind() &&
340  D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
341  // For function templates and class templates, the template is numbered and
342  // not its pattern.
343  if (auto *FD = dyn_cast<FunctionDecl>(D))
344  return !FD->getDescribedFunctionTemplate();
345  if (auto *RD = dyn_cast<CXXRecordDecl>(D))
346  return !RD->getDescribedClassTemplate();
347  return true;
348  }
349 
350  // Otherwise, we only care about anonymous class members.
351  if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
352  return false;
353  return isa<TagDecl>(D) || isa<FieldDecl>(D);
354 }
355 
The ObjC &#39;SEL&#39; type.
Definition: ASTBitCodes.h:889
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:241
TagDecl * getDefinition() const
Returns the TagDecl that actually defines this struct/union/class/enum.
Definition: Decl.cpp:3812
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:2235
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
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:182
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
ObjCProtocolDecl * getDefinition()
Retrieve the definition of this protocol, if any.
Definition: DeclObjC.h:2229
The &#39;unsigned short&#39; type.
Definition: ASTBitCodes.h:820
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:2083
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC)...
Definition: DeclBase.h:826
The type of &#39;nullptr&#39;.
Definition: ASTBitCodes.h:874
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 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 isDependentContext() const
Determines whether this context is dependent on a template parameter.
Definition: DeclBase.cpp:1040
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 placeholder type for bound member functions.
Definition: ASTBitCodes.h:895
The &#39;unsigned long long&#39; type.
Definition: ASTBitCodes.h:829
Decl::Kind getDeclKind() const
Definition: DeclBase.h:1334
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.
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3006
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
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:170
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:1262
The placeholder type for dependent types.
Definition: ASTBitCodes.h:865
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:332
This class is used for builtin types like &#39;int&#39;.
Definition: Type.h:2214
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
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