blob: 0159c1648c05162bf315c98864b947906df381d6 [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;
70 case BuiltinType::OCLImage3d: ID = PREDEF_TYPE_IMAGE3D_ID; break;
Guy Benyei61054192013-02-07 10:55:47 +000071 case BuiltinType::OCLSampler: ID = PREDEF_TYPE_SAMPLER_ID; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +000072 case BuiltinType::OCLEvent: ID = PREDEF_TYPE_EVENT_ID; break;
Guy Benyei11169dd2012-12-18 14:30:41 +000073 case BuiltinType::BuiltinFn:
74 ID = PREDEF_TYPE_BUILTIN_FN; break;
Alexey Bataev1a3320e2015-08-25 14:24:04 +000075 case BuiltinType::OMPArraySection: ID = PREDEF_TYPE_OMP_ARRAY_SECTION; break;
Guy Benyei11169dd2012-12-18 14:30:41 +000076
77 }
78
79 return TypeIdx(ID);
80}
81
82unsigned serialization::ComputeHash(Selector Sel) {
83 unsigned N = Sel.getNumArgs();
84 if (N == 0)
85 ++N;
86 unsigned R = 5381;
87 for (unsigned I = 0; I != N; ++I)
88 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
89 R = llvm::HashString(II->getName(), R);
90 return R;
91}
Douglas Gregor9f782892013-01-21 15:25:38 +000092
Douglas Gregor7a6e2002013-01-22 17:08:30 +000093const DeclContext *
94serialization::getDefinitiveDeclContext(const DeclContext *DC) {
Douglas Gregor9f782892013-01-21 15:25:38 +000095 switch (DC->getDeclKind()) {
96 // These entities may have multiple definitions.
97 case Decl::TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +000098 case Decl::ExternCContext:
Douglas Gregor9f782892013-01-21 15:25:38 +000099 case Decl::Namespace:
100 case Decl::LinkageSpec:
Craig Toppera13603a2014-05-22 05:54:18 +0000101 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000102
103 // C/C++ tag types can only be defined in one place.
104 case Decl::Enum:
105 case Decl::Record:
106 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
107 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000108 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000109
110 // FIXME: These can be defined in one place... except special member
111 // functions and out-of-line definitions.
112 case Decl::CXXRecord:
113 case Decl::ClassTemplateSpecialization:
114 case Decl::ClassTemplatePartialSpecialization:
Craig Toppera13603a2014-05-22 05:54:18 +0000115 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000116
117 // Each function, method, and block declaration is its own DeclContext.
118 case Decl::Function:
119 case Decl::CXXMethod:
120 case Decl::CXXConstructor:
121 case Decl::CXXDestructor:
122 case Decl::CXXConversion:
123 case Decl::ObjCMethod:
124 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000125 case Decl::Captured:
Douglas Gregor9f782892013-01-21 15:25:38 +0000126 // Objective C categories, category implementations, and class
127 // implementations can only be defined in one place.
128 case Decl::ObjCCategory:
129 case Decl::ObjCCategoryImpl:
130 case Decl::ObjCImplementation:
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000131 return DC;
Douglas Gregor9f782892013-01-21 15:25:38 +0000132
133 case Decl::ObjCProtocol:
134 if (const ObjCProtocolDecl *Def
135 = cast<ObjCProtocolDecl>(DC)->getDefinition())
136 return Def;
Craig Toppera13603a2014-05-22 05:54:18 +0000137 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +0000138
139 // FIXME: These are defined in one place, but properties in class extensions
140 // end up being back-patched into the main interface. See
141 // Sema::HandlePropertyInClassExtension for the offending code.
142 case Decl::ObjCInterface:
Craig Toppera13603a2014-05-22 05:54:18 +0000143 return nullptr;
144
Douglas Gregor9f782892013-01-21 15:25:38 +0000145 default:
146 llvm_unreachable("Unhandled DeclContext in AST reader");
147 }
148
Douglas Gregor7a6e2002013-01-22 17:08:30 +0000149 llvm_unreachable("Unhandled decl kind");
Douglas Gregorfe732d52013-01-21 16:16:40 +0000150}
Douglas Gregor9f782892013-01-21 15:25:38 +0000151
Douglas Gregorfe732d52013-01-21 16:16:40 +0000152bool serialization::isRedeclarableDeclKind(unsigned Kind) {
153 switch (static_cast<Decl::Kind>(Kind)) {
Richard Smithf19e1272015-03-07 00:04:49 +0000154 case Decl::TranslationUnit:
155 case Decl::ExternCContext:
156 // Special case of a "merged" declaration.
157 return true;
158
Douglas Gregorfe732d52013-01-21 16:16:40 +0000159 case Decl::Namespace:
Richard Smithf4634362014-09-03 23:11:22 +0000160 case Decl::NamespaceAlias:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000161 case Decl::Typedef:
162 case Decl::TypeAlias:
163 case Decl::Enum:
164 case Decl::Record:
165 case Decl::CXXRecord:
166 case Decl::ClassTemplateSpecialization:
167 case Decl::ClassTemplatePartialSpecialization:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000168 case Decl::VarTemplateSpecialization:
169 case Decl::VarTemplatePartialSpecialization:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000170 case Decl::Function:
171 case Decl::CXXMethod:
172 case Decl::CXXConstructor:
173 case Decl::CXXDestructor:
174 case Decl::CXXConversion:
Richard Smithfd8634a2013-10-23 02:17:46 +0000175 case Decl::UsingShadow:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000176 case Decl::Var:
177 case Decl::FunctionTemplate:
178 case Decl::ClassTemplate:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000179 case Decl::VarTemplate:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000180 case Decl::TypeAliasTemplate:
181 case Decl::ObjCProtocol:
182 case Decl::ObjCInterface:
Michael Han84324352013-02-22 17:15:32 +0000183 case Decl::Empty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000184 return true;
185
186 // Never redeclarable.
187 case Decl::UsingDirective:
188 case Decl::Label:
189 case Decl::UnresolvedUsingTypename:
190 case Decl::TemplateTypeParm:
191 case Decl::EnumConstant:
192 case Decl::UnresolvedUsingValue:
193 case Decl::IndirectField:
194 case Decl::Field:
John McCall5e77d762013-04-16 07:28:30 +0000195 case Decl::MSProperty:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000196 case Decl::ObjCIvar:
197 case Decl::ObjCAtDefsField:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000198 case Decl::NonTypeTemplateParm:
199 case Decl::TemplateTemplateParm:
200 case Decl::Using:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000201 case Decl::ObjCMethod:
202 case Decl::ObjCCategory:
203 case Decl::ObjCCategoryImpl:
204 case Decl::ObjCImplementation:
205 case Decl::ObjCProperty:
206 case Decl::ObjCCompatibleAlias:
207 case Decl::LinkageSpec:
208 case Decl::ObjCPropertyImpl:
209 case Decl::FileScopeAsm:
210 case Decl::AccessSpec:
211 case Decl::Friend:
212 case Decl::FriendTemplate:
213 case Decl::StaticAssert:
214 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000215 case Decl::Captured:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000216 case Decl::ClassScopeFunctionSpecialization:
217 case Decl::Import:
Alexey Bataeva769e072013-03-22 06:34:35 +0000218 case Decl::OMPThreadPrivate:
Douglas Gregorfe732d52013-01-21 16:16:40 +0000219 return false;
Richard Smithf4634362014-09-03 23:11:22 +0000220
221 // These indirectly derive from Redeclarable<T> but are not actually
222 // redeclarable.
223 case Decl::ImplicitParam:
224 case Decl::ParmVar:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000225 case Decl::ObjCTypeParam:
Richard Smithf4634362014-09-03 23:11:22 +0000226 return false;
Douglas Gregorfe732d52013-01-21 16:16:40 +0000227 }
228
229 llvm_unreachable("Unhandled declaration kind");
Douglas Gregor9f782892013-01-21 15:25:38 +0000230}
Richard Smithd08aeb62014-08-28 01:33:39 +0000231
232bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
Richard Smith2b560572015-02-07 03:11:11 +0000233 // Friend declarations in dependent contexts aren't anonymous in the usual
234 // sense, but they cannot be found by name lookup in their semantic context
235 // (or indeed in any context), so we treat them as anonymous.
236 //
237 // This doesn't apply to friend tag decls; Sema makes those available to name
238 // lookup in the surrounding context.
239 if (D->getFriendObjectKind() &&
240 D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
241 // For function templates and class templates, the template is numbered and
242 // not its pattern.
243 if (auto *FD = dyn_cast<FunctionDecl>(D))
244 return !FD->getDescribedFunctionTemplate();
245 if (auto *RD = dyn_cast<CXXRecordDecl>(D))
246 return !RD->getDescribedClassTemplate();
247 return true;
248 }
249
250 // Otherwise, we only care about anonymous class members.
Richard Smithd08aeb62014-08-28 01:33:39 +0000251 if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
252 return false;
253 return isa<TagDecl>(D) || isa<FieldDecl>(D);
254}
255