clang  3.8.0
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/ADT/StringExtras.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::NullPtr:
96  break;
97  case BuiltinType::Char16:
99  break;
100  case BuiltinType::Char32:
102  break;
103  case BuiltinType::Overload:
105  break;
106  case BuiltinType::BoundMember:
108  break;
109  case BuiltinType::PseudoObject:
111  break;
112  case BuiltinType::Dependent:
114  break;
115  case BuiltinType::UnknownAny:
117  break;
118  case BuiltinType::ARCUnbridgedCast:
120  break;
121  case BuiltinType::ObjCId:
122  ID = PREDEF_TYPE_OBJC_ID;
123  break;
124  case BuiltinType::ObjCClass:
126  break;
127  case BuiltinType::ObjCSel:
129  break;
130  case BuiltinType::OCLImage1d:
132  break;
133  case BuiltinType::OCLImage1dArray:
135  break;
136  case BuiltinType::OCLImage1dBuffer:
138  break;
139  case BuiltinType::OCLImage2d:
141  break;
142  case BuiltinType::OCLImage2dArray:
144  break;
145  case BuiltinType::OCLImage2dDepth:
147  break;
148  case BuiltinType::OCLImage2dArrayDepth:
150  break;
151  case BuiltinType::OCLImage2dMSAA:
153  break;
154  case BuiltinType::OCLImage2dArrayMSAA:
156  break;
157  case BuiltinType::OCLImage2dMSAADepth:
159  break;
160  case BuiltinType::OCLImage2dArrayMSAADepth:
162  break;
163  case BuiltinType::OCLImage3d:
165  break;
166  case BuiltinType::OCLSampler:
168  break;
169  case BuiltinType::OCLEvent:
171  break;
172  case BuiltinType::OCLClkEvent:
174  break;
175  case BuiltinType::OCLQueue:
177  break;
178  case BuiltinType::OCLNDRange:
180  break;
181  case BuiltinType::OCLReserveID:
183  break;
184  case BuiltinType::BuiltinFn:
186  break;
187  case BuiltinType::OMPArraySection:
189  break;
190  }
191 
192  return TypeIdx(ID);
193 }
194 
196  unsigned N = Sel.getNumArgs();
197  if (N == 0)
198  ++N;
199  unsigned R = 5381;
200  for (unsigned I = 0; I != N; ++I)
202  R = llvm::HashString(II->getName(), R);
203  return R;
204 }
205 
206 const DeclContext *
208  switch (DC->getDeclKind()) {
209  // These entities may have multiple definitions.
210  case Decl::TranslationUnit:
211  case Decl::ExternCContext:
212  case Decl::Namespace:
213  case Decl::LinkageSpec:
214  return nullptr;
215 
216  // C/C++ tag types can only be defined in one place.
217  case Decl::Enum:
218  case Decl::Record:
219  if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
220  return Def;
221  return nullptr;
222 
223  // FIXME: These can be defined in one place... except special member
224  // functions and out-of-line definitions.
225  case Decl::CXXRecord:
226  case Decl::ClassTemplateSpecialization:
227  case Decl::ClassTemplatePartialSpecialization:
228  return nullptr;
229 
230  // Each function, method, and block declaration is its own DeclContext.
231  case Decl::Function:
232  case Decl::CXXMethod:
233  case Decl::CXXConstructor:
234  case Decl::CXXDestructor:
235  case Decl::CXXConversion:
236  case Decl::ObjCMethod:
237  case Decl::Block:
238  case Decl::Captured:
239  // Objective C categories, category implementations, and class
240  // implementations can only be defined in one place.
241  case Decl::ObjCCategory:
242  case Decl::ObjCCategoryImpl:
243  case Decl::ObjCImplementation:
244  return DC;
245 
246  case Decl::ObjCProtocol:
247  if (const ObjCProtocolDecl *Def
248  = cast<ObjCProtocolDecl>(DC)->getDefinition())
249  return Def;
250  return nullptr;
251 
252  // FIXME: These are defined in one place, but properties in class extensions
253  // end up being back-patched into the main interface. See
254  // Sema::HandlePropertyInClassExtension for the offending code.
255  case Decl::ObjCInterface:
256  return nullptr;
257 
258  default:
259  llvm_unreachable("Unhandled DeclContext in AST reader");
260  }
261 
262  llvm_unreachable("Unhandled decl kind");
263 }
264 
266  switch (static_cast<Decl::Kind>(Kind)) {
267  case Decl::TranslationUnit:
268  case Decl::ExternCContext:
269  // Special case of a "merged" declaration.
270  return true;
271 
272  case Decl::Namespace:
273  case Decl::NamespaceAlias:
274  case Decl::Typedef:
275  case Decl::TypeAlias:
276  case Decl::Enum:
277  case Decl::Record:
278  case Decl::CXXRecord:
279  case Decl::ClassTemplateSpecialization:
280  case Decl::ClassTemplatePartialSpecialization:
281  case Decl::VarTemplateSpecialization:
282  case Decl::VarTemplatePartialSpecialization:
283  case Decl::Function:
284  case Decl::CXXMethod:
285  case Decl::CXXConstructor:
286  case Decl::CXXDestructor:
287  case Decl::CXXConversion:
288  case Decl::UsingShadow:
289  case Decl::Var:
290  case Decl::FunctionTemplate:
291  case Decl::ClassTemplate:
292  case Decl::VarTemplate:
293  case Decl::TypeAliasTemplate:
294  case Decl::ObjCProtocol:
295  case Decl::ObjCInterface:
296  case Decl::Empty:
297  return true;
298 
299  // Never redeclarable.
300  case Decl::UsingDirective:
301  case Decl::Label:
302  case Decl::UnresolvedUsingTypename:
303  case Decl::TemplateTypeParm:
304  case Decl::EnumConstant:
305  case Decl::UnresolvedUsingValue:
306  case Decl::IndirectField:
307  case Decl::Field:
308  case Decl::MSProperty:
309  case Decl::ObjCIvar:
310  case Decl::ObjCAtDefsField:
311  case Decl::NonTypeTemplateParm:
312  case Decl::TemplateTemplateParm:
313  case Decl::Using:
314  case Decl::ObjCMethod:
315  case Decl::ObjCCategory:
316  case Decl::ObjCCategoryImpl:
317  case Decl::ObjCImplementation:
318  case Decl::ObjCProperty:
319  case Decl::ObjCCompatibleAlias:
320  case Decl::LinkageSpec:
321  case Decl::ObjCPropertyImpl:
322  case Decl::FileScopeAsm:
323  case Decl::AccessSpec:
324  case Decl::Friend:
325  case Decl::FriendTemplate:
326  case Decl::StaticAssert:
327  case Decl::Block:
328  case Decl::Captured:
329  case Decl::ClassScopeFunctionSpecialization:
330  case Decl::Import:
331  case Decl::OMPThreadPrivate:
332  case Decl::BuiltinTemplate:
333  return false;
334 
335  // These indirectly derive from Redeclarable<T> but are not actually
336  // redeclarable.
337  case Decl::ImplicitParam:
338  case Decl::ParmVar:
339  case Decl::ObjCTypeParam:
340  return false;
341  }
342 
343  llvm_unreachable("Unhandled declaration kind");
344 }
345 
347  // Friend declarations in dependent contexts aren't anonymous in the usual
348  // sense, but they cannot be found by name lookup in their semantic context
349  // (or indeed in any context), so we treat them as anonymous.
350  //
351  // This doesn't apply to friend tag decls; Sema makes those available to name
352  // lookup in the surrounding context.
353  if (D->getFriendObjectKind() &&
354  D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
355  // For function templates and class templates, the template is numbered and
356  // not its pattern.
357  if (auto *FD = dyn_cast<FunctionDecl>(D))
358  return !FD->getDescribedFunctionTemplate();
359  if (auto *RD = dyn_cast<CXXRecordDecl>(D))
360  return !RD->getDescribedClassTemplate();
361  return true;
362  }
363 
364  // Otherwise, we only care about anonymous class members.
365  if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
366  return false;
367  return isa<TagDecl>(D) || isa<FieldDecl>(D);
368 }
369 
Kind getKind() const
Definition: Type.h:2028
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:265
The (signed) 'long long' type.
Definition: ASTBitCodes.h:732
The 'bool' or '_Bool' type.
Definition: ASTBitCodes.h:706
The 'unknown any' placeholder type.
Definition: ASTBitCodes.h:760
The placeholder type for builtin functions.
Definition: ASTBitCodes.h:774
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
Definition: DeclBase.cpp:884
ARC's unbridged-cast placeholder type.
Definition: ASTBitCodes.h:770
const DeclContext * getDefinitiveDeclContext(const DeclContext *DC)
Retrieve the "definitive" declaration that provides all of the visible entries for the given declarat...
Definition: ASTCommon.cpp:207
One of these records is kept for each identifier that is lexed.
IdentifierInfo * getIdentifierInfoForSlot(unsigned argIndex) const
Retrieve the identifier at a given position in the selector.
The 'char' type, when it is signed.
Definition: ASTBitCodes.h:720
ObjCProtocolDecl * getDefinition()
Retrieve the definition of this protocol, if any.
Definition: DeclObjC.h:1858
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:1728
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC)...
Definition: DeclBase.h:708
detail::InMemoryDirectory::const_iterator I
The placeholder type for OpenMP array section.
Definition: ASTBitCodes.h:812
OpenCL 2d image array depth type.
Definition: ASTBitCodes.h:788
ID
Defines the set of possible language-specific address spaces.
Definition: AddressSpaces.h:27
The 'char' type, when it is unsigned.
Definition: ASTBitCodes.h:708
unsigned getNumArgs() const
The placeholder type for overloaded function sets.
Definition: ASTBitCodes.h:740
The OpenCL 'half' / ARM NEON __fp16 type.
Definition: ASTBitCodes.h:768
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
DeclarationName getDeclName() const
getDeclName - Get the actual, stored name of the declaration, which may be a special name...
Definition: Decl.h:190
TagDecl * getDefinition() const
getDefinition - Returns the TagDecl that actually defines this struct/union/class/enum.
Definition: Decl.cpp:3561
The placeholder type for bound member functions.
Definition: ASTBitCodes.h:762
The 'unsigned long long' type.
Definition: ASTBitCodes.h:718
Kind
TagDecl - Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:2644
TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT)
Definition: ASTCommon.cpp:27
unsigned ComputeHash(Selector Sel)
Definition: ASTCommon.cpp:195
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1121
The placeholder type for dependent types.
Definition: ASTBitCodes.h:742
bool TypeAlias
Whether this template specialization type is a substituted type alias.
Definition: Type.h:4005
Decl::Kind getDeclKind() const
Definition: DeclBase.h:1194
FriendObjectKind getFriendObjectKind() const
Determines whether this declaration is the object of a friend declaration and, if so...
Definition: DeclBase.h:978
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
bool needsAnonymousDeclarationNumber(const NamedDecl *D)
Determine whether the given declaration needs an anonymous declaration number.
Definition: ASTCommon.cpp:346
This class is used for builtin types like 'int'.
Definition: Type.h:2011
OpenCL 2d image array MSAA depth type.
Definition: ASTBitCodes.h:796
NamedDecl - This represents a decl with a name.
Definition: Decl.h:145
A type index; the type ID with the qualifier bits removed.
Definition: ASTBitCodes.h:83
The pseudo-object placeholder type.
Definition: ASTBitCodes.h:772