• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Serialization/ASTDeserializationListener.h"
19 #include "llvm/ADT/StringExtras.h"
20 
21 using namespace clang;
22 
23 // Give ASTDeserializationListener's VTable a home.
~ASTDeserializationListener()24 ASTDeserializationListener::~ASTDeserializationListener() { }
25 
26 serialization::TypeIdx
TypeIdxFromBuiltin(const BuiltinType * BT)27 serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
28   unsigned ID = 0;
29   switch (BT->getKind()) {
30   case BuiltinType::Void:
31     ID = PREDEF_TYPE_VOID_ID;
32     break;
33   case BuiltinType::Bool:
34     ID = PREDEF_TYPE_BOOL_ID;
35     break;
36   case BuiltinType::Char_U:
37     ID = PREDEF_TYPE_CHAR_U_ID;
38     break;
39   case BuiltinType::UChar:
40     ID = PREDEF_TYPE_UCHAR_ID;
41     break;
42   case BuiltinType::UShort:
43     ID = PREDEF_TYPE_USHORT_ID;
44     break;
45   case BuiltinType::UInt:
46     ID = PREDEF_TYPE_UINT_ID;
47     break;
48   case BuiltinType::ULong:
49     ID = PREDEF_TYPE_ULONG_ID;
50     break;
51   case BuiltinType::ULongLong:
52     ID = PREDEF_TYPE_ULONGLONG_ID;
53     break;
54   case BuiltinType::UInt128:
55     ID = PREDEF_TYPE_UINT128_ID;
56     break;
57   case BuiltinType::Char_S:
58     ID = PREDEF_TYPE_CHAR_S_ID;
59     break;
60   case BuiltinType::SChar:
61     ID = PREDEF_TYPE_SCHAR_ID;
62     break;
63   case BuiltinType::WChar_S:
64   case BuiltinType::WChar_U:
65     ID = PREDEF_TYPE_WCHAR_ID;
66     break;
67   case BuiltinType::Short:
68     ID = PREDEF_TYPE_SHORT_ID;
69     break;
70   case BuiltinType::Int:
71     ID = PREDEF_TYPE_INT_ID;
72     break;
73   case BuiltinType::Long:
74     ID = PREDEF_TYPE_LONG_ID;
75     break;
76   case BuiltinType::LongLong:
77     ID = PREDEF_TYPE_LONGLONG_ID;
78     break;
79   case BuiltinType::Int128:
80     ID = PREDEF_TYPE_INT128_ID;
81     break;
82   case BuiltinType::Half:
83     ID = PREDEF_TYPE_HALF_ID;
84     break;
85   case BuiltinType::Float:
86     ID = PREDEF_TYPE_FLOAT_ID;
87     break;
88   case BuiltinType::Double:
89     ID = PREDEF_TYPE_DOUBLE_ID;
90     break;
91   case BuiltinType::LongDouble:
92     ID = PREDEF_TYPE_LONGDOUBLE_ID;
93     break;
94   case BuiltinType::Float128:
95     ID = PREDEF_TYPE_FLOAT128_ID;
96     break;
97   case BuiltinType::NullPtr:
98     ID = PREDEF_TYPE_NULLPTR_ID;
99     break;
100   case BuiltinType::Char16:
101     ID = PREDEF_TYPE_CHAR16_ID;
102     break;
103   case BuiltinType::Char32:
104     ID = PREDEF_TYPE_CHAR32_ID;
105     break;
106   case BuiltinType::Overload:
107     ID = PREDEF_TYPE_OVERLOAD_ID;
108     break;
109   case BuiltinType::BoundMember:
110     ID = PREDEF_TYPE_BOUND_MEMBER;
111     break;
112   case BuiltinType::PseudoObject:
113     ID = PREDEF_TYPE_PSEUDO_OBJECT;
114     break;
115   case BuiltinType::Dependent:
116     ID = PREDEF_TYPE_DEPENDENT_ID;
117     break;
118   case BuiltinType::UnknownAny:
119     ID = PREDEF_TYPE_UNKNOWN_ANY;
120     break;
121   case BuiltinType::ARCUnbridgedCast:
122     ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;
123     break;
124   case BuiltinType::ObjCId:
125     ID = PREDEF_TYPE_OBJC_ID;
126     break;
127   case BuiltinType::ObjCClass:
128     ID = PREDEF_TYPE_OBJC_CLASS;
129     break;
130   case BuiltinType::ObjCSel:
131     ID = PREDEF_TYPE_OBJC_SEL;
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:
139     ID = PREDEF_TYPE_SAMPLER_ID;
140     break;
141   case BuiltinType::OCLEvent:
142     ID = PREDEF_TYPE_EVENT_ID;
143     break;
144   case BuiltinType::OCLClkEvent:
145     ID = PREDEF_TYPE_CLK_EVENT_ID;
146     break;
147   case BuiltinType::OCLQueue:
148     ID = PREDEF_TYPE_QUEUE_ID;
149     break;
150   case BuiltinType::OCLNDRange:
151     ID = PREDEF_TYPE_NDRANGE_ID;
152     break;
153   case BuiltinType::OCLReserveID:
154     ID = PREDEF_TYPE_RESERVE_ID_ID;
155     break;
156   case BuiltinType::BuiltinFn:
157     ID = PREDEF_TYPE_BUILTIN_FN;
158     break;
159   case BuiltinType::OMPArraySection:
160     ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
161     break;
162   }
163 
164   return TypeIdx(ID);
165 }
166 
ComputeHash(Selector Sel)167 unsigned serialization::ComputeHash(Selector Sel) {
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 *
getDefinitiveDeclContext(const DeclContext * DC)179 serialization::getDefinitiveDeclContext(const DeclContext *DC) {
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     return nullptr;
187 
188   // C/C++ tag types can only be defined in one place.
189   case Decl::Enum:
190   case Decl::Record:
191     if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
192       return Def;
193     return nullptr;
194 
195   // FIXME: These can be defined in one place... except special member
196   // functions and out-of-line definitions.
197   case Decl::CXXRecord:
198   case Decl::ClassTemplateSpecialization:
199   case Decl::ClassTemplatePartialSpecialization:
200     return nullptr;
201 
202   // Each function, method, and block declaration is its own DeclContext.
203   case Decl::Function:
204   case Decl::CXXMethod:
205   case Decl::CXXConstructor:
206   case Decl::CXXDestructor:
207   case Decl::CXXConversion:
208   case Decl::ObjCMethod:
209   case Decl::Block:
210   case Decl::Captured:
211     // Objective C categories, category implementations, and class
212     // implementations can only be defined in one place.
213   case Decl::ObjCCategory:
214   case Decl::ObjCCategoryImpl:
215   case Decl::ObjCImplementation:
216     return DC;
217 
218   case Decl::ObjCProtocol:
219     if (const ObjCProtocolDecl *Def
220           = cast<ObjCProtocolDecl>(DC)->getDefinition())
221       return Def;
222     return nullptr;
223 
224   // FIXME: These are defined in one place, but properties in class extensions
225   // end up being back-patched into the main interface. See
226   // Sema::HandlePropertyInClassExtension for the offending code.
227   case Decl::ObjCInterface:
228     return nullptr;
229 
230   default:
231     llvm_unreachable("Unhandled DeclContext in AST reader");
232   }
233 
234   llvm_unreachable("Unhandled decl kind");
235 }
236 
isRedeclarableDeclKind(unsigned Kind)237 bool serialization::isRedeclarableDeclKind(unsigned Kind) {
238   switch (static_cast<Decl::Kind>(Kind)) {
239   case Decl::TranslationUnit:
240   case Decl::ExternCContext:
241     // Special case of a "merged" declaration.
242     return true;
243 
244   case Decl::Namespace:
245   case Decl::NamespaceAlias:
246   case Decl::Typedef:
247   case Decl::TypeAlias:
248   case Decl::Enum:
249   case Decl::Record:
250   case Decl::CXXRecord:
251   case Decl::ClassTemplateSpecialization:
252   case Decl::ClassTemplatePartialSpecialization:
253   case Decl::VarTemplateSpecialization:
254   case Decl::VarTemplatePartialSpecialization:
255   case Decl::Function:
256   case Decl::CXXMethod:
257   case Decl::CXXConstructor:
258   case Decl::CXXDestructor:
259   case Decl::CXXConversion:
260   case Decl::UsingShadow:
261   case Decl::ConstructorUsingShadow:
262   case Decl::Var:
263   case Decl::FunctionTemplate:
264   case Decl::ClassTemplate:
265   case Decl::VarTemplate:
266   case Decl::TypeAliasTemplate:
267   case Decl::ObjCProtocol:
268   case Decl::ObjCInterface:
269   case Decl::Empty:
270     return true;
271 
272   // Never redeclarable.
273   case Decl::UsingDirective:
274   case Decl::Label:
275   case Decl::UnresolvedUsingTypename:
276   case Decl::TemplateTypeParm:
277   case Decl::EnumConstant:
278   case Decl::UnresolvedUsingValue:
279   case Decl::IndirectField:
280   case Decl::Field:
281   case Decl::MSProperty:
282   case Decl::ObjCIvar:
283   case Decl::ObjCAtDefsField:
284   case Decl::NonTypeTemplateParm:
285   case Decl::TemplateTemplateParm:
286   case Decl::Using:
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::ObjCPropertyImpl:
295   case Decl::PragmaComment:
296   case Decl::PragmaDetectMismatch:
297   case Decl::FileScopeAsm:
298   case Decl::AccessSpec:
299   case Decl::Friend:
300   case Decl::FriendTemplate:
301   case Decl::StaticAssert:
302   case Decl::Block:
303   case Decl::Captured:
304   case Decl::ClassScopeFunctionSpecialization:
305   case Decl::Import:
306   case Decl::OMPThreadPrivate:
307   case Decl::OMPCapturedExpr:
308   case Decl::OMPDeclareReduction:
309   case Decl::BuiltinTemplate:
310     return false;
311 
312   // These indirectly derive from Redeclarable<T> but are not actually
313   // redeclarable.
314   case Decl::ImplicitParam:
315   case Decl::ParmVar:
316   case Decl::ObjCTypeParam:
317     return false;
318   }
319 
320   llvm_unreachable("Unhandled declaration kind");
321 }
322 
needsAnonymousDeclarationNumber(const NamedDecl * D)323 bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
324   // Friend declarations in dependent contexts aren't anonymous in the usual
325   // sense, but they cannot be found by name lookup in their semantic context
326   // (or indeed in any context), so we treat them as anonymous.
327   //
328   // This doesn't apply to friend tag decls; Sema makes those available to name
329   // lookup in the surrounding context.
330   if (D->getFriendObjectKind() &&
331       D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
332     // For function templates and class templates, the template is numbered and
333     // not its pattern.
334     if (auto *FD = dyn_cast<FunctionDecl>(D))
335       return !FD->getDescribedFunctionTemplate();
336     if (auto *RD = dyn_cast<CXXRecordDecl>(D))
337       return !RD->getDescribedClassTemplate();
338     return true;
339   }
340 
341   // Otherwise, we only care about anonymous class members.
342   if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
343     return false;
344   return isa<TagDecl>(D) || isa<FieldDecl>(D);
345 }
346 
347