blob: 04425acb4c5d43c3c22b11120b7f119a9e0adeff [file] [log] [blame]
Guy Benyei7f92f2d2012-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"
Stephen Hines176edba2014-12-01 14:53:08 -080015#include "clang/AST/DeclCXX.h"
Douglas Gregor5a04f9f2013-01-21 15:25:38 +000016#include "clang/AST/DeclObjC.h"
Guy Benyei7f92f2d2012-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 Benyeib13621d2012-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 Benyei21f18c42013-02-07 10:55:47 +000071 case BuiltinType::OCLSampler: ID = PREDEF_TYPE_SAMPLER_ID; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +000072 case BuiltinType::OCLEvent: ID = PREDEF_TYPE_EVENT_ID; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000073 case BuiltinType::BuiltinFn:
74 ID = PREDEF_TYPE_BUILTIN_FN; break;
75
76 }
77
78 return TypeIdx(ID);
79}
80
81unsigned serialization::ComputeHash(Selector Sel) {
82 unsigned N = Sel.getNumArgs();
83 if (N == 0)
84 ++N;
85 unsigned R = 5381;
86 for (unsigned I = 0; I != N; ++I)
87 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
88 R = llvm::HashString(II->getName(), R);
89 return R;
90}
Douglas Gregor5a04f9f2013-01-21 15:25:38 +000091
Douglas Gregore0d20662013-01-22 17:08:30 +000092const DeclContext *
93serialization::getDefinitiveDeclContext(const DeclContext *DC) {
Douglas Gregor5a04f9f2013-01-21 15:25:38 +000094 switch (DC->getDeclKind()) {
95 // These entities may have multiple definitions.
96 case Decl::TranslationUnit:
97 case Decl::Namespace:
98 case Decl::LinkageSpec:
Stephen Hines6bcf27b2014-05-29 04:14:42 -070099 return nullptr;
Douglas Gregor5a04f9f2013-01-21 15:25:38 +0000100
101 // C/C++ tag types can only be defined in one place.
102 case Decl::Enum:
103 case Decl::Record:
104 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
105 return Def;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700106 return nullptr;
Douglas Gregor5a04f9f2013-01-21 15:25:38 +0000107
108 // FIXME: These can be defined in one place... except special member
109 // functions and out-of-line definitions.
110 case Decl::CXXRecord:
111 case Decl::ClassTemplateSpecialization:
112 case Decl::ClassTemplatePartialSpecialization:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700113 return nullptr;
Douglas Gregor5a04f9f2013-01-21 15:25:38 +0000114
115 // Each function, method, and block declaration is its own DeclContext.
116 case Decl::Function:
117 case Decl::CXXMethod:
118 case Decl::CXXConstructor:
119 case Decl::CXXDestructor:
120 case Decl::CXXConversion:
121 case Decl::ObjCMethod:
122 case Decl::Block:
Tareq A. Siraj6afcf882013-04-16 19:37:38 +0000123 case Decl::Captured:
Douglas Gregor5a04f9f2013-01-21 15:25:38 +0000124 // Objective C categories, category implementations, and class
125 // implementations can only be defined in one place.
126 case Decl::ObjCCategory:
127 case Decl::ObjCCategoryImpl:
128 case Decl::ObjCImplementation:
Douglas Gregore0d20662013-01-22 17:08:30 +0000129 return DC;
Douglas Gregor5a04f9f2013-01-21 15:25:38 +0000130
131 case Decl::ObjCProtocol:
132 if (const ObjCProtocolDecl *Def
133 = cast<ObjCProtocolDecl>(DC)->getDefinition())
134 return Def;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700135 return nullptr;
Douglas Gregor5a04f9f2013-01-21 15:25:38 +0000136
137 // FIXME: These are defined in one place, but properties in class extensions
138 // end up being back-patched into the main interface. See
139 // Sema::HandlePropertyInClassExtension for the offending code.
140 case Decl::ObjCInterface:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700141 return nullptr;
142
Douglas Gregor5a04f9f2013-01-21 15:25:38 +0000143 default:
144 llvm_unreachable("Unhandled DeclContext in AST reader");
145 }
146
Douglas Gregore0d20662013-01-22 17:08:30 +0000147 llvm_unreachable("Unhandled decl kind");
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000148}
Douglas Gregor5a04f9f2013-01-21 15:25:38 +0000149
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000150bool serialization::isRedeclarableDeclKind(unsigned Kind) {
151 switch (static_cast<Decl::Kind>(Kind)) {
152 case Decl::TranslationUnit: // Special case of a "merged" declaration.
153 case Decl::Namespace:
Stephen Hines176edba2014-12-01 14:53:08 -0800154 case Decl::NamespaceAlias:
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000155 case Decl::Typedef:
156 case Decl::TypeAlias:
157 case Decl::Enum:
158 case Decl::Record:
159 case Decl::CXXRecord:
160 case Decl::ClassTemplateSpecialization:
161 case Decl::ClassTemplatePartialSpecialization:
Larisse Voufoef4579c2013-08-06 01:03:05 +0000162 case Decl::VarTemplateSpecialization:
163 case Decl::VarTemplatePartialSpecialization:
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000164 case Decl::Function:
165 case Decl::CXXMethod:
166 case Decl::CXXConstructor:
167 case Decl::CXXDestructor:
168 case Decl::CXXConversion:
Richard Smithf06a28932013-10-23 02:17:46 +0000169 case Decl::UsingShadow:
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000170 case Decl::Var:
171 case Decl::FunctionTemplate:
172 case Decl::ClassTemplate:
Larisse Voufoef4579c2013-08-06 01:03:05 +0000173 case Decl::VarTemplate:
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000174 case Decl::TypeAliasTemplate:
175 case Decl::ObjCProtocol:
176 case Decl::ObjCInterface:
Michael Han684aa732013-02-22 17:15:32 +0000177 case Decl::Empty:
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000178 return true;
179
180 // Never redeclarable.
181 case Decl::UsingDirective:
182 case Decl::Label:
183 case Decl::UnresolvedUsingTypename:
184 case Decl::TemplateTypeParm:
185 case Decl::EnumConstant:
186 case Decl::UnresolvedUsingValue:
187 case Decl::IndirectField:
188 case Decl::Field:
John McCall76da55d2013-04-16 07:28:30 +0000189 case Decl::MSProperty:
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000190 case Decl::ObjCIvar:
191 case Decl::ObjCAtDefsField:
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000192 case Decl::NonTypeTemplateParm:
193 case Decl::TemplateTemplateParm:
194 case Decl::Using:
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000195 case Decl::ObjCMethod:
196 case Decl::ObjCCategory:
197 case Decl::ObjCCategoryImpl:
198 case Decl::ObjCImplementation:
199 case Decl::ObjCProperty:
200 case Decl::ObjCCompatibleAlias:
201 case Decl::LinkageSpec:
202 case Decl::ObjCPropertyImpl:
203 case Decl::FileScopeAsm:
204 case Decl::AccessSpec:
205 case Decl::Friend:
206 case Decl::FriendTemplate:
207 case Decl::StaticAssert:
208 case Decl::Block:
Tareq A. Siraj6afcf882013-04-16 19:37:38 +0000209 case Decl::Captured:
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000210 case Decl::ClassScopeFunctionSpecialization:
211 case Decl::Import:
Alexey Bataevc6400582013-03-22 06:34:35 +0000212 case Decl::OMPThreadPrivate:
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000213 return false;
Stephen Hines176edba2014-12-01 14:53:08 -0800214
215 // These indirectly derive from Redeclarable<T> but are not actually
216 // redeclarable.
217 case Decl::ImplicitParam:
218 case Decl::ParmVar:
219 return false;
Douglas Gregor9cfdc032013-01-21 16:16:40 +0000220 }
221
222 llvm_unreachable("Unhandled declaration kind");
Douglas Gregor5a04f9f2013-01-21 15:25:38 +0000223}
Stephen Hines176edba2014-12-01 14:53:08 -0800224
225bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700226 // Friend declarations in dependent contexts aren't anonymous in the usual
227 // sense, but they cannot be found by name lookup in their semantic context
228 // (or indeed in any context), so we treat them as anonymous.
229 //
230 // This doesn't apply to friend tag decls; Sema makes those available to name
231 // lookup in the surrounding context.
232 if (D->getFriendObjectKind() &&
233 D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
234 // For function templates and class templates, the template is numbered and
235 // not its pattern.
236 if (auto *FD = dyn_cast<FunctionDecl>(D))
237 return !FD->getDescribedFunctionTemplate();
238 if (auto *RD = dyn_cast<CXXRecordDecl>(D))
239 return !RD->getDescribedClassTemplate();
240 return true;
241 }
242
243 // Otherwise, we only care about anonymous class members.
Stephen Hines176edba2014-12-01 14:53:08 -0800244 if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
245 return false;
246 return isa<TagDecl>(D) || isa<FieldDecl>(D);
247}
248