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