blob: b117581a2d2d9e334b64ba477313d1000968049b [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- 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"
Richard Smithd08aeb62014-08-28 01:33:39 +000015#include "clang/AST/DeclCXX.h"
Douglas Gregor9f782892013-01-21 15:25:38 +000016#include "clang/AST/DeclObjC.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "clang/Basic/IdentifierTable.h"
18#include "clang/Serialization/ASTDeserializationListener.h"
19#include "llvm/ADT/StringExtras.h"
20
21using namespace clang;
22
23// Give ASTDeserializationListener's VTable a home.
24ASTDeserializationListener::~ASTDeserializationListener() { }
25
26serialization::TypeIdx
27serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
28 unsigned ID = 0;
29 switch (BT->getKind()) {
30 case BuiltinType::Void: ID = PREDEF_TYPE_VOID_ID; break;
31 case BuiltinType::Bool: ID = PREDEF_TYPE_BOOL_ID; break;
32 case BuiltinType::Char_U: ID = PREDEF_TYPE_CHAR_U_ID; break;
33 case BuiltinType::UChar: ID = PREDEF_TYPE_UCHAR_ID; break;
34 case BuiltinType::UShort: ID = PREDEF_TYPE_USHORT_ID; break;
35 case BuiltinType::UInt: ID = PREDEF_TYPE_UINT_ID; break;
36 case BuiltinType::ULong: ID = PREDEF_TYPE_ULONG_ID; break;
37 case BuiltinType::ULongLong: ID = PREDEF_TYPE_ULONGLONG_ID; break;
38 case BuiltinType::UInt128: ID = PREDEF_TYPE_UINT128_ID; break;
39 case BuiltinType::Char_S: ID = PREDEF_TYPE_CHAR_S_ID; break;
40 case BuiltinType::SChar: ID = PREDEF_TYPE_SCHAR_ID; break;
41 case BuiltinType::WChar_S:
42 case BuiltinType::WChar_U: ID = PREDEF_TYPE_WCHAR_ID; break;
43 case BuiltinType::Short: ID = PREDEF_TYPE_SHORT_ID; break;
44 case BuiltinType::Int: ID = PREDEF_TYPE_INT_ID; break;
45 case BuiltinType::Long: ID = PREDEF_TYPE_LONG_ID; break;
46 case BuiltinType::LongLong: ID = PREDEF_TYPE_LONGLONG_ID; break;
47 case BuiltinType::Int128: ID = PREDEF_TYPE_INT128_ID; break;
48 case BuiltinType::Half: ID = PREDEF_TYPE_HALF_ID; break;
49 case BuiltinType::Float: ID = PREDEF_TYPE_FLOAT_ID; break;
50 case BuiltinType::Double: ID = PREDEF_TYPE_DOUBLE_ID; break;
51 case BuiltinType::LongDouble: ID = PREDEF_TYPE_LONGDOUBLE_ID; break;
52 case BuiltinType::NullPtr: ID = PREDEF_TYPE_NULLPTR_ID; break;
53 case BuiltinType::Char16: ID = PREDEF_TYPE_CHAR16_ID; break;
54 case BuiltinType::Char32: ID = PREDEF_TYPE_CHAR32_ID; break;
55 case BuiltinType::Overload: ID = PREDEF_TYPE_OVERLOAD_ID; break;
56 case BuiltinType::BoundMember:ID = PREDEF_TYPE_BOUND_MEMBER; break;
57 case BuiltinType::PseudoObject:ID = PREDEF_TYPE_PSEUDO_OBJECT;break;
58 case BuiltinType::Dependent: ID = PREDEF_TYPE_DEPENDENT_ID; break;
59 case BuiltinType::UnknownAny: ID = PREDEF_TYPE_UNKNOWN_ANY; break;
60 case BuiltinType::ARCUnbridgedCast:
61 ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST; break;
62 case BuiltinType::ObjCId: ID = PREDEF_TYPE_OBJC_ID; break;
63 case BuiltinType::ObjCClass: ID = PREDEF_TYPE_OBJC_CLASS; break;
64 case BuiltinType::ObjCSel: ID = PREDEF_TYPE_OBJC_SEL; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +000065 case BuiltinType::OCLImage1d: ID = PREDEF_TYPE_IMAGE1D_ID; break;
66 case BuiltinType::OCLImage1dArray: ID = PREDEF_TYPE_IMAGE1D_ARR_ID; break;
67 case BuiltinType::OCLImage1dBuffer: ID = PREDEF_TYPE_IMAGE1D_BUFF_ID; break;
68 case BuiltinType::OCLImage2d: ID = PREDEF_TYPE_IMAGE2D_ID; break;
69 case BuiltinType::OCLImage2dArray: ID = PREDEF_TYPE_IMAGE2D_ARR_ID; break;
Alexey Bader9c8453f2015-09-15 11:18:52 +000070 case BuiltinType::OCLImage2dDepth:
71 ID = PREDEF_TYPE_IMAGE2D_DEP_ID;
72 break;
73 case BuiltinType::OCLImage2dArrayDepth:
74 ID = PREDEF_TYPE_IMAGE2D_ARR_DEP_ID;
75 break;
76 case BuiltinType::OCLImage2dMSAA:
77 ID = PREDEF_TYPE_IMAGE2D_MSAA_ID;
78 break;
79 case BuiltinType::OCLImage2dArrayMSAA:
80 ID = PREDEF_TYPE_IMAGE2D_ARR_MSAA_ID;
81 break;
82 case BuiltinType::OCLImage2dMSAADepth:
83 ID = PREDEF_TYPE_IMAGE2D_MSAA_DEP_ID;
84 break;
85 case BuiltinType::OCLImage2dArrayMSAADepth:
86 ID = PREDEF_TYPE_IMAGE2D_ARR_MSAA_DEPTH_ID;
87 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +000088 case BuiltinType::OCLImage3d: ID = PREDEF_TYPE_IMAGE3D_ID; break;
Guy Benyei61054192013-02-07 10:55:47 +000089 case BuiltinType::OCLSampler: ID = PREDEF_TYPE_SAMPLER_ID; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +000090 case BuiltinType::OCLEvent: ID = PREDEF_TYPE_EVENT_ID; break;
Alexey Bader9c8453f2015-09-15 11:18:52 +000091 case BuiltinType::OCLClkEvent:
92 ID = PREDEF_TYPE_CLK_EVENT_ID;
93 break;
94 case BuiltinType::OCLQueue:
95 ID = PREDEF_TYPE_QUEUE_ID;
96 break;
97 case BuiltinType::OCLNDRange:
98 ID = PREDEF_TYPE_NDRANGE_ID;
99 break;
100 case BuiltinType::OCLReserveID:
101 ID = PREDEF_TYPE_RESERVE_ID_ID;
102 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000103 case BuiltinType::BuiltinFn:
104 ID = PREDEF_TYPE_BUILTIN_FN; break;
Alexey Bataev1a3320e2015-08-25 14:24:04 +0000105 case BuiltinType::OMPArraySection: ID = PREDEF_TYPE_OMP_ARRAY_SECTION; break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000106
107 }
108
109 return TypeIdx(ID);
110}
111
112unsigned serialization::ComputeHash(Selector Sel) {
113 unsigned N = Sel.getNumArgs();
114 if (N == 0)
115 ++N;
116 unsigned R = 5381;
117 for (unsigned I = 0; I != N; ++I)
118 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
119 R = llvm::HashString(II->getName(), R);
120 return R;
121}
Douglas Gregor9f782892013-01-21 15:25:38 +0000122
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000123const DeclContext *
124serialization::getDefinitiveDeclContext(const DeclContext *DC) {
Douglas Gregor9f782892013-01-21 15:25:38 +0000125 switch (DC->getDeclKind()) {
126 // These entities may have multiple definitions.
127 case Decl::TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +0000128 case Decl::ExternCContext:
Douglas Gregor9f782892013-01-21 15:25:38 +0000129 case Decl::Namespace:
130 case Decl::LinkageSpec:
Craig Toppera13603a2014-05-22 05:54:18 +0000131 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000132
133 // C/C++ tag types can only be defined in one place.
134 case Decl::Enum:
135 case Decl::Record:
136 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
137 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000138 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000139
140 // FIXME: These can be defined in one place... except special member
141 // functions and out-of-line definitions.
142 case Decl::CXXRecord:
143 case Decl::ClassTemplateSpecialization:
144 case Decl::ClassTemplatePartialSpecialization:
Craig Toppera13603a2014-05-22 05:54:18 +0000145 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000146
147 // Each function, method, and block declaration is its own DeclContext.
148 case Decl::Function:
149 case Decl::CXXMethod:
150 case Decl::CXXConstructor:
151 case Decl::CXXDestructor:
152 case Decl::CXXConversion:
153 case Decl::ObjCMethod:
154 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000155 case Decl::Captured:
Douglas Gregor9f782892013-01-21 15:25:38 +0000156 // Objective C categories, category implementations, and class
157 // implementations can only be defined in one place.
158 case Decl::ObjCCategory:
159 case Decl::ObjCCategoryImpl:
160 case Decl::ObjCImplementation:
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000161 return DC;
Douglas Gregor9f782892013-01-21 15:25:38 +0000162
163 case Decl::ObjCProtocol:
164 if (const ObjCProtocolDecl *Def
165 = cast<ObjCProtocolDecl>(DC)->getDefinition())
166 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000167 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000168
169 // FIXME: These are defined in one place, but properties in class extensions
170 // end up being back-patched into the main interface. See
171 // Sema::HandlePropertyInClassExtension for the offending code.
172 case Decl::ObjCInterface:
Craig Toppera13603a2014-05-22 05:54:18 +0000173 return nullptr;
174
Douglas Gregor9f782892013-01-21 15:25:38 +0000175 default:
176 llvm_unreachable("Unhandled DeclContext in AST reader");
177 }
178
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000179 llvm_unreachable("Unhandled decl kind");
Douglas Gregorfe732d52013-01-21 16:16:40 +0000180}
Douglas Gregor9f782892013-01-21 15:25:38 +0000181
Douglas Gregorfe732d52013-01-21 16:16:40 +0000182bool serialization::isRedeclarableDeclKind(unsigned Kind) {
183 switch (static_cast<Decl::Kind>(Kind)) {
Richard Smithf19e1272015-03-07 00:04:49 +0000184 case Decl::TranslationUnit:
185 case Decl::ExternCContext:
186 // Special case of a "merged" declaration.
187 return true;
188
Douglas Gregorfe732d52013-01-21 16:16:40 +0000189 case Decl::Namespace:
Richard Smithf4634362014-09-03 23:11:22 +0000190 case Decl::NamespaceAlias:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000191 case Decl::Typedef:
192 case Decl::TypeAlias:
193 case Decl::Enum:
194 case Decl::Record:
195 case Decl::CXXRecord:
196 case Decl::ClassTemplateSpecialization:
197 case Decl::ClassTemplatePartialSpecialization:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000198 case Decl::VarTemplateSpecialization:
199 case Decl::VarTemplatePartialSpecialization:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000200 case Decl::Function:
201 case Decl::CXXMethod:
202 case Decl::CXXConstructor:
203 case Decl::CXXDestructor:
204 case Decl::CXXConversion:
Richard Smithfd8634a2013-10-23 02:17:46 +0000205 case Decl::UsingShadow:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000206 case Decl::Var:
207 case Decl::FunctionTemplate:
208 case Decl::ClassTemplate:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000209 case Decl::VarTemplate:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000210 case Decl::TypeAliasTemplate:
211 case Decl::ObjCProtocol:
212 case Decl::ObjCInterface:
Michael Han84324352013-02-22 17:15:32 +0000213 case Decl::Empty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000214 return true;
215
216 // Never redeclarable.
217 case Decl::UsingDirective:
218 case Decl::Label:
219 case Decl::UnresolvedUsingTypename:
220 case Decl::TemplateTypeParm:
221 case Decl::EnumConstant:
222 case Decl::UnresolvedUsingValue:
223 case Decl::IndirectField:
224 case Decl::Field:
John McCall5e77d762013-04-16 07:28:30 +0000225 case Decl::MSProperty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000226 case Decl::ObjCIvar:
227 case Decl::ObjCAtDefsField:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000228 case Decl::NonTypeTemplateParm:
229 case Decl::TemplateTemplateParm:
230 case Decl::Using:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000231 case Decl::ObjCMethod:
232 case Decl::ObjCCategory:
233 case Decl::ObjCCategoryImpl:
234 case Decl::ObjCImplementation:
235 case Decl::ObjCProperty:
236 case Decl::ObjCCompatibleAlias:
237 case Decl::LinkageSpec:
238 case Decl::ObjCPropertyImpl:
239 case Decl::FileScopeAsm:
240 case Decl::AccessSpec:
241 case Decl::Friend:
242 case Decl::FriendTemplate:
243 case Decl::StaticAssert:
244 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000245 case Decl::Captured:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000246 case Decl::ClassScopeFunctionSpecialization:
247 case Decl::Import:
Alexey Bataeva769e072013-03-22 06:34:35 +0000248 case Decl::OMPThreadPrivate:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000249 return false;
Richard Smithf4634362014-09-03 23:11:22 +0000250
251 // These indirectly derive from Redeclarable<T> but are not actually
252 // redeclarable.
253 case Decl::ImplicitParam:
254 case Decl::ParmVar:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000255 case Decl::ObjCTypeParam:
Richard Smithf4634362014-09-03 23:11:22 +0000256 return false;
Douglas Gregorfe732d52013-01-21 16:16:40 +0000257 }
258
259 llvm_unreachable("Unhandled declaration kind");
Douglas Gregor9f782892013-01-21 15:25:38 +0000260}
Richard Smithd08aeb62014-08-28 01:33:39 +0000261
262bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
Richard Smith2b560572015-02-07 03:11:11 +0000263 // Friend declarations in dependent contexts aren't anonymous in the usual
264 // sense, but they cannot be found by name lookup in their semantic context
265 // (or indeed in any context), so we treat them as anonymous.
266 //
267 // This doesn't apply to friend tag decls; Sema makes those available to name
268 // lookup in the surrounding context.
269 if (D->getFriendObjectKind() &&
270 D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
271 // For function templates and class templates, the template is numbered and
272 // not its pattern.
273 if (auto *FD = dyn_cast<FunctionDecl>(D))
274 return !FD->getDescribedFunctionTemplate();
275 if (auto *RD = dyn_cast<CXXRecordDecl>(D))
276 return !RD->getDescribedClassTemplate();
277 return true;
278 }
279
280 // Otherwise, we only care about anonymous class members.
Richard Smithd08aeb62014-08-28 01:33:39 +0000281 if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
282 return false;
283 return isa<TagDecl>(D) || isa<FieldDecl>(D);
284}
285