]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Serialization/ASTCommon.cpp
Merge from head
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Serialization / ASTCommon.cpp
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.
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:       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;
65   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;
71   case BuiltinType::OCLSampler:       ID = PREDEF_TYPE_SAMPLER_ID;      break;
72   case BuiltinType::OCLEvent:         ID = PREDEF_TYPE_EVENT_ID;        break;
73   case BuiltinType::BuiltinFn:
74                                 ID = PREDEF_TYPE_BUILTIN_FN; break;
75
76   }
77
78   return TypeIdx(ID);
79 }
80
81 unsigned 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 }
91
92 const DeclContext *
93 serialization::getDefinitiveDeclContext(const DeclContext *DC) {
94   switch (DC->getDeclKind()) {
95   // These entities may have multiple definitions.
96   case Decl::TranslationUnit:
97   case Decl::ExternCContext:
98   case Decl::Namespace:
99   case Decl::LinkageSpec:
100     return nullptr;
101
102   // C/C++ tag types can only be defined in one place.
103   case Decl::Enum:
104   case Decl::Record:
105     if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
106       return Def;
107     return nullptr;
108
109   // FIXME: These can be defined in one place... except special member
110   // functions and out-of-line definitions.
111   case Decl::CXXRecord:
112   case Decl::ClassTemplateSpecialization:
113   case Decl::ClassTemplatePartialSpecialization:
114     return nullptr;
115
116   // Each function, method, and block declaration is its own DeclContext.
117   case Decl::Function:
118   case Decl::CXXMethod:
119   case Decl::CXXConstructor:
120   case Decl::CXXDestructor:
121   case Decl::CXXConversion:
122   case Decl::ObjCMethod:
123   case Decl::Block:
124   case Decl::Captured:
125     // Objective C categories, category implementations, and class
126     // implementations can only be defined in one place.
127   case Decl::ObjCCategory:
128   case Decl::ObjCCategoryImpl:
129   case Decl::ObjCImplementation:
130     return DC;
131
132   case Decl::ObjCProtocol:
133     if (const ObjCProtocolDecl *Def
134           = cast<ObjCProtocolDecl>(DC)->getDefinition())
135       return Def;
136     return nullptr;
137
138   // FIXME: These are defined in one place, but properties in class extensions
139   // end up being back-patched into the main interface. See
140   // Sema::HandlePropertyInClassExtension for the offending code.
141   case Decl::ObjCInterface:
142     return nullptr;
143
144   default:
145     llvm_unreachable("Unhandled DeclContext in AST reader");
146   }
147   
148   llvm_unreachable("Unhandled decl kind");
149 }
150
151 bool serialization::isRedeclarableDeclKind(unsigned Kind) {
152   switch (static_cast<Decl::Kind>(Kind)) {
153   case Decl::TranslationUnit:
154   case Decl::ExternCContext:
155     // Special case of a "merged" declaration.
156     return true;
157
158   case Decl::Namespace:
159   case Decl::NamespaceAlias:
160   case Decl::Typedef:
161   case Decl::TypeAlias:
162   case Decl::Enum:
163   case Decl::Record:
164   case Decl::CXXRecord:
165   case Decl::ClassTemplateSpecialization:
166   case Decl::ClassTemplatePartialSpecialization:
167   case Decl::VarTemplateSpecialization:
168   case Decl::VarTemplatePartialSpecialization:
169   case Decl::Function:
170   case Decl::CXXMethod:
171   case Decl::CXXConstructor:
172   case Decl::CXXDestructor:
173   case Decl::CXXConversion:
174   case Decl::UsingShadow:
175   case Decl::Var:
176   case Decl::FunctionTemplate:
177   case Decl::ClassTemplate:
178   case Decl::VarTemplate:
179   case Decl::TypeAliasTemplate:
180   case Decl::ObjCProtocol:
181   case Decl::ObjCInterface:
182   case Decl::Empty:
183     return true;
184
185   // Never redeclarable.
186   case Decl::UsingDirective:
187   case Decl::Label:
188   case Decl::UnresolvedUsingTypename:
189   case Decl::TemplateTypeParm:
190   case Decl::EnumConstant:
191   case Decl::UnresolvedUsingValue:
192   case Decl::IndirectField:
193   case Decl::Field:
194   case Decl::MSProperty:
195   case Decl::ObjCIvar:
196   case Decl::ObjCAtDefsField:
197   case Decl::NonTypeTemplateParm:
198   case Decl::TemplateTemplateParm:
199   case Decl::Using:
200   case Decl::ObjCMethod:
201   case Decl::ObjCCategory:
202   case Decl::ObjCCategoryImpl:
203   case Decl::ObjCImplementation:
204   case Decl::ObjCProperty:
205   case Decl::ObjCCompatibleAlias:
206   case Decl::LinkageSpec:
207   case Decl::ObjCPropertyImpl:
208   case Decl::FileScopeAsm:
209   case Decl::AccessSpec:
210   case Decl::Friend:
211   case Decl::FriendTemplate:
212   case Decl::StaticAssert:
213   case Decl::Block:
214   case Decl::Captured:
215   case Decl::ClassScopeFunctionSpecialization:
216   case Decl::Import:
217   case Decl::OMPThreadPrivate:
218     return false;
219
220   // These indirectly derive from Redeclarable<T> but are not actually
221   // redeclarable.
222   case Decl::ImplicitParam:
223   case Decl::ParmVar:
224   case Decl::ObjCTypeParam:
225     return false;
226   }
227
228   llvm_unreachable("Unhandled declaration kind");
229 }
230
231 bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
232   // Friend declarations in dependent contexts aren't anonymous in the usual
233   // sense, but they cannot be found by name lookup in their semantic context
234   // (or indeed in any context), so we treat them as anonymous.
235   //
236   // This doesn't apply to friend tag decls; Sema makes those available to name
237   // lookup in the surrounding context.
238   if (D->getFriendObjectKind() &&
239       D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
240     // For function templates and class templates, the template is numbered and
241     // not its pattern.
242     if (auto *FD = dyn_cast<FunctionDecl>(D))
243       return !FD->getDescribedFunctionTemplate();
244     if (auto *RD = dyn_cast<CXXRecordDecl>(D))
245       return !RD->getDescribedClassTemplate();
246     return true;
247   }
248
249   // Otherwise, we only care about anonymous class members.
250   if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
251     return false;
252   return isa<TagDecl>(D) || isa<FieldDecl>(D);
253 }
254