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/Support/DJB.h"
20 
21 using namespace clang;
22 
23 // Give ASTDeserializationListener's VTable a home.
24 ASTDeserializationListener::~ASTDeserializationListener() { }
25 
26 serialization::TypeIdx
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::ShortAccum:
95     ID = PREDEF_TYPE_SHORT_ACCUM_ID;
96     break;
97   case BuiltinType::Accum:
98     ID = PREDEF_TYPE_ACCUM_ID;
99     break;
100   case BuiltinType::LongAccum:
101     ID = PREDEF_TYPE_LONG_ACCUM_ID;
102     break;
103   case BuiltinType::UShortAccum:
104     ID = PREDEF_TYPE_USHORT_ACCUM_ID;
105     break;
106   case BuiltinType::UAccum:
107     ID = PREDEF_TYPE_UACCUM_ID;
108     break;
109   case BuiltinType::ULongAccum:
110     ID = PREDEF_TYPE_ULONG_ACCUM_ID;
111     break;
112   case BuiltinType::Float16:
113     ID = PREDEF_TYPE_FLOAT16_ID;
114     break;
115   case BuiltinType::Float128:
116     ID = PREDEF_TYPE_FLOAT128_ID;
117     break;
118   case BuiltinType::NullPtr:
119     ID = PREDEF_TYPE_NULLPTR_ID;
120     break;
121   case BuiltinType::Char8:
122     ID = PREDEF_TYPE_CHAR8_ID;
123     break;
124   case BuiltinType::Char16:
125     ID = PREDEF_TYPE_CHAR16_ID;
126     break;
127   case BuiltinType::Char32:
128     ID = PREDEF_TYPE_CHAR32_ID;
129     break;
130   case BuiltinType::Overload:
131     ID = PREDEF_TYPE_OVERLOAD_ID;
132     break;
133   case BuiltinType::BoundMember:
134     ID = PREDEF_TYPE_BOUND_MEMBER;
135     break;
136   case BuiltinType::PseudoObject:
137     ID = PREDEF_TYPE_PSEUDO_OBJECT;
138     break;
139   case BuiltinType::Dependent:
140     ID = PREDEF_TYPE_DEPENDENT_ID;
141     break;
142   case BuiltinType::UnknownAny:
143     ID = PREDEF_TYPE_UNKNOWN_ANY;
144     break;
145   case BuiltinType::ARCUnbridgedCast:
146     ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;
147     break;
148   case BuiltinType::ObjCId:
149     ID = PREDEF_TYPE_OBJC_ID;
150     break;
151   case BuiltinType::ObjCClass:
152     ID = PREDEF_TYPE_OBJC_CLASS;
153     break;
154   case BuiltinType::ObjCSel:
155     ID = PREDEF_TYPE_OBJC_SEL;
156     break;
157 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
158   case BuiltinType::Id: \
159     ID = PREDEF_TYPE_##Id##_ID; \
160     break;
161 #include "clang/Basic/OpenCLImageTypes.def"
162   case BuiltinType::OCLSampler:
163     ID = PREDEF_TYPE_SAMPLER_ID;
164     break;
165   case BuiltinType::OCLEvent:
166     ID = PREDEF_TYPE_EVENT_ID;
167     break;
168   case BuiltinType::OCLClkEvent:
169     ID = PREDEF_TYPE_CLK_EVENT_ID;
170     break;
171   case BuiltinType::OCLQueue:
172     ID = PREDEF_TYPE_QUEUE_ID;
173     break;
174   case BuiltinType::OCLReserveID:
175     ID = PREDEF_TYPE_RESERVE_ID_ID;
176     break;
177   case BuiltinType::BuiltinFn:
178     ID = PREDEF_TYPE_BUILTIN_FN;
179     break;
180   case BuiltinType::OMPArraySection:
181     ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
182     break;
183   }
184 
185   return TypeIdx(ID);
186 }
187 
188 unsigned serialization::ComputeHash(Selector Sel) {
189   unsigned N = Sel.getNumArgs();
190   if (N == 0)
191     ++N;
192   unsigned R = 5381;
193   for (unsigned I = 0; I != N; ++I)
194     if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
195       R = llvm::djbHash(II->getName(), R);
196   return R;
197 }
198 
199 const DeclContext *
200 serialization::getDefinitiveDeclContext(const DeclContext *DC) {
201   switch (DC->getDeclKind()) {
202   // These entities may have multiple definitions.
203   case Decl::TranslationUnit:
204   case Decl::ExternCContext:
205   case Decl::Namespace:
206   case Decl::LinkageSpec:
207   case Decl::Export:
208     return nullptr;
209 
210   // C/C++ tag types can only be defined in one place.
211   case Decl::Enum:
212   case Decl::Record:
213     if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
214       return Def;
215     return nullptr;
216 
217   // FIXME: These can be defined in one place... except special member
218   // functions and out-of-line definitions.
219   case Decl::CXXRecord:
220   case Decl::ClassTemplateSpecialization:
221   case Decl::ClassTemplatePartialSpecialization:
222     return nullptr;
223 
224   // Each function, method, and block declaration is its own DeclContext.
225   case Decl::Function:
226   case Decl::CXXMethod:
227   case Decl::CXXConstructor:
228   case Decl::CXXDestructor:
229   case Decl::CXXConversion:
230   case Decl::ObjCMethod:
231   case Decl::Block:
232   case Decl::Captured:
233     // Objective C categories, category implementations, and class
234     // implementations can only be defined in one place.
235   case Decl::ObjCCategory:
236   case Decl::ObjCCategoryImpl:
237   case Decl::ObjCImplementation:
238     return DC;
239 
240   case Decl::ObjCProtocol:
241     if (const ObjCProtocolDecl *Def
242           = cast<ObjCProtocolDecl>(DC)->getDefinition())
243       return Def;
244     return nullptr;
245 
246   // FIXME: These are defined in one place, but properties in class extensions
247   // end up being back-patched into the main interface. See
248   // Sema::HandlePropertyInClassExtension for the offending code.
249   case Decl::ObjCInterface:
250     return nullptr;
251 
252   default:
253     llvm_unreachable("Unhandled DeclContext in AST reader");
254   }
255 
256   llvm_unreachable("Unhandled decl kind");
257 }
258 
259 bool serialization::isRedeclarableDeclKind(unsigned Kind) {
260   switch (static_cast<Decl::Kind>(Kind)) {
261   case Decl::TranslationUnit:
262   case Decl::ExternCContext:
263     // Special case of a "merged" declaration.
264     return true;
265 
266   case Decl::Namespace:
267   case Decl::NamespaceAlias:
268   case Decl::Typedef:
269   case Decl::TypeAlias:
270   case Decl::Enum:
271   case Decl::Record:
272   case Decl::CXXRecord:
273   case Decl::ClassTemplateSpecialization:
274   case Decl::ClassTemplatePartialSpecialization:
275   case Decl::VarTemplateSpecialization:
276   case Decl::VarTemplatePartialSpecialization:
277   case Decl::Function:
278   case Decl::CXXDeductionGuide:
279   case Decl::CXXMethod:
280   case Decl::CXXConstructor:
281   case Decl::CXXDestructor:
282   case Decl::CXXConversion:
283   case Decl::UsingShadow:
284   case Decl::ConstructorUsingShadow:
285   case Decl::Var:
286   case Decl::FunctionTemplate:
287   case Decl::ClassTemplate:
288   case Decl::VarTemplate:
289   case Decl::TypeAliasTemplate:
290   case Decl::ObjCProtocol:
291   case Decl::ObjCInterface:
292   case Decl::Empty:
293     return true;
294 
295   // Never redeclarable.
296   case Decl::UsingDirective:
297   case Decl::Label:
298   case Decl::UnresolvedUsingTypename:
299   case Decl::TemplateTypeParm:
300   case Decl::EnumConstant:
301   case Decl::UnresolvedUsingValue:
302   case Decl::IndirectField:
303   case Decl::Field:
304   case Decl::MSProperty:
305   case Decl::ObjCIvar:
306   case Decl::ObjCAtDefsField:
307   case Decl::NonTypeTemplateParm:
308   case Decl::TemplateTemplateParm:
309   case Decl::Using:
310   case Decl::UsingPack:
311   case Decl::ObjCMethod:
312   case Decl::ObjCCategory:
313   case Decl::ObjCCategoryImpl:
314   case Decl::ObjCImplementation:
315   case Decl::ObjCProperty:
316   case Decl::ObjCCompatibleAlias:
317   case Decl::LinkageSpec:
318   case Decl::Export:
319   case Decl::ObjCPropertyImpl:
320   case Decl::PragmaComment:
321   case Decl::PragmaDetectMismatch:
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::OMPCapturedExpr:
333   case Decl::OMPDeclareReduction:
334   case Decl::BuiltinTemplate:
335   case Decl::Decomposition:
336   case Decl::Binding:
337     return false;
338 
339   // These indirectly derive from Redeclarable<T> but are not actually
340   // redeclarable.
341   case Decl::ImplicitParam:
342   case Decl::ParmVar:
343   case Decl::ObjCTypeParam:
344     return false;
345   }
346 
347   llvm_unreachable("Unhandled declaration kind");
348 }
349 
350 bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
351   // Friend declarations in dependent contexts aren't anonymous in the usual
352   // sense, but they cannot be found by name lookup in their semantic context
353   // (or indeed in any context), so we treat them as anonymous.
354   //
355   // This doesn't apply to friend tag decls; Sema makes those available to name
356   // lookup in the surrounding context.
357   if (D->getFriendObjectKind() &&
358       D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
359     // For function templates and class templates, the template is numbered and
360     // not its pattern.
361     if (auto *FD = dyn_cast<FunctionDecl>(D))
362       return !FD->getDescribedFunctionTemplate();
363     if (auto *RD = dyn_cast<CXXRecordDecl>(D))
364       return !RD->getDescribedClassTemplate();
365     return true;
366   }
367 
368   // Otherwise, we only care about anonymous class members.
369   if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
370     return false;
371   return isa<TagDecl>(D) || isa<FieldDecl>(D);
372 }
373 
374