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