17f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei//===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===//
27f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei//
37f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei//                     The LLVM Compiler Infrastructure
47f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei//
57f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei// This file is distributed under the University of Illinois Open Source
67f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei// License. See LICENSE.TXT for details.
77f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei//
87f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei//===----------------------------------------------------------------------===//
97f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei//
107f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei//  This file defines common functions that both ASTReader and ASTWriter use.
117f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei//
127f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei//===----------------------------------------------------------------------===//
137f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei
147f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei#include "ASTCommon.h"
15176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "clang/AST/DeclCXX.h"
165a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor#include "clang/AST/DeclObjC.h"
177f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei#include "clang/Basic/IdentifierTable.h"
187f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei#include "clang/Serialization/ASTDeserializationListener.h"
197f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei#include "llvm/ADT/StringExtras.h"
207f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei
217f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyeiusing namespace clang;
227f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei
237f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei// Give ASTDeserializationListener's VTable a home.
247f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy BenyeiASTDeserializationListener::~ASTDeserializationListener() { }
257f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei
267f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyeiserialization::TypeIdx
277f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyeiserialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
287f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  unsigned ID = 0;
297f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  switch (BT->getKind()) {
307f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Void:       ID = PREDEF_TYPE_VOID_ID;       break;
317f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Bool:       ID = PREDEF_TYPE_BOOL_ID;       break;
327f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Char_U:     ID = PREDEF_TYPE_CHAR_U_ID;     break;
337f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::UChar:      ID = PREDEF_TYPE_UCHAR_ID;      break;
347f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::UShort:     ID = PREDEF_TYPE_USHORT_ID;     break;
357f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::UInt:       ID = PREDEF_TYPE_UINT_ID;       break;
367f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::ULong:      ID = PREDEF_TYPE_ULONG_ID;      break;
377f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::ULongLong:  ID = PREDEF_TYPE_ULONGLONG_ID;  break;
387f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::UInt128:    ID = PREDEF_TYPE_UINT128_ID;    break;
397f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Char_S:     ID = PREDEF_TYPE_CHAR_S_ID;     break;
407f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::SChar:      ID = PREDEF_TYPE_SCHAR_ID;      break;
417f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::WChar_S:
427f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::WChar_U:    ID = PREDEF_TYPE_WCHAR_ID;      break;
437f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Short:      ID = PREDEF_TYPE_SHORT_ID;      break;
447f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Int:        ID = PREDEF_TYPE_INT_ID;        break;
457f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Long:       ID = PREDEF_TYPE_LONG_ID;       break;
467f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::LongLong:   ID = PREDEF_TYPE_LONGLONG_ID;   break;
477f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Int128:     ID = PREDEF_TYPE_INT128_ID;     break;
487f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Half:       ID = PREDEF_TYPE_HALF_ID;       break;
497f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Float:      ID = PREDEF_TYPE_FLOAT_ID;      break;
507f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Double:     ID = PREDEF_TYPE_DOUBLE_ID;     break;
517f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::LongDouble: ID = PREDEF_TYPE_LONGDOUBLE_ID; break;
527f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::NullPtr:    ID = PREDEF_TYPE_NULLPTR_ID;    break;
537f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Char16:     ID = PREDEF_TYPE_CHAR16_ID;     break;
547f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Char32:     ID = PREDEF_TYPE_CHAR32_ID;     break;
557f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Overload:   ID = PREDEF_TYPE_OVERLOAD_ID;   break;
567f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::BoundMember:ID = PREDEF_TYPE_BOUND_MEMBER;  break;
577f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::PseudoObject:ID = PREDEF_TYPE_PSEUDO_OBJECT;break;
587f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::Dependent:  ID = PREDEF_TYPE_DEPENDENT_ID;  break;
597f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::UnknownAny: ID = PREDEF_TYPE_UNKNOWN_ANY;   break;
607f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::ARCUnbridgedCast:
617f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei                                ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST; break;
627f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::ObjCId:     ID = PREDEF_TYPE_OBJC_ID;       break;
637f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::ObjCClass:  ID = PREDEF_TYPE_OBJC_CLASS;    break;
647f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::ObjCSel:    ID = PREDEF_TYPE_OBJC_SEL;      break;
65b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case BuiltinType::OCLImage1d:       ID = PREDEF_TYPE_IMAGE1D_ID;      break;
66b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case BuiltinType::OCLImage1dArray:  ID = PREDEF_TYPE_IMAGE1D_ARR_ID;  break;
67b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case BuiltinType::OCLImage1dBuffer: ID = PREDEF_TYPE_IMAGE1D_BUFF_ID; break;
68b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case BuiltinType::OCLImage2d:       ID = PREDEF_TYPE_IMAGE2D_ID;      break;
69b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case BuiltinType::OCLImage2dArray:  ID = PREDEF_TYPE_IMAGE2D_ARR_ID;  break;
70b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case BuiltinType::OCLImage3d:       ID = PREDEF_TYPE_IMAGE3D_ID;      break;
7121f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei  case BuiltinType::OCLSampler:       ID = PREDEF_TYPE_SAMPLER_ID;      break;
72e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei  case BuiltinType::OCLEvent:         ID = PREDEF_TYPE_EVENT_ID;        break;
737f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  case BuiltinType::BuiltinFn:
747f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei                                ID = PREDEF_TYPE_BUILTIN_FN; break;
757f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei
767f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  }
777f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei
787f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  return TypeIdx(ID);
797f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei}
807f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei
817f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyeiunsigned serialization::ComputeHash(Selector Sel) {
827f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  unsigned N = Sel.getNumArgs();
837f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  if (N == 0)
847f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei    ++N;
857f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  unsigned R = 5381;
867f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  for (unsigned I = 0; I != N; ++I)
877f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei    if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
887f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei      R = llvm::HashString(II->getName(), R);
897f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei  return R;
907f92f2d8d9b7a07900c030183bc13a9ff60057ccGuy Benyei}
915a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor
92e0d2066ab8812cbaa16d79af5f2c9fb6bd52d2e2Douglas Gregorconst DeclContext *
93e0d2066ab8812cbaa16d79af5f2c9fb6bd52d2e2Douglas Gregorserialization::getDefinitiveDeclContext(const DeclContext *DC) {
945a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  switch (DC->getDeclKind()) {
955a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  // These entities may have multiple definitions.
965a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::TranslationUnit:
973ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  case Decl::ExternCContext:
985a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::Namespace:
995a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::LinkageSpec:
1006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
1015a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor
1025a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  // C/C++ tag types can only be defined in one place.
1035a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::Enum:
1045a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::Record:
1055a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor    if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
1065a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor      return Def;
1076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
1085a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor
1095a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  // FIXME: These can be defined in one place... except special member
1105a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  // functions and out-of-line definitions.
1115a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::CXXRecord:
1125a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::ClassTemplateSpecialization:
1135a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::ClassTemplatePartialSpecialization:
1146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
1155a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor
1165a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  // Each function, method, and block declaration is its own DeclContext.
1175a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::Function:
1185a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::CXXMethod:
1195a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::CXXConstructor:
1205a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::CXXDestructor:
1215a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::CXXConversion:
1225a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::ObjCMethod:
1235a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::Block:
1246afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  case Decl::Captured:
1255a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor    // Objective C categories, category implementations, and class
1265a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor    // implementations can only be defined in one place.
1275a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::ObjCCategory:
1285a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::ObjCCategoryImpl:
1295a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::ObjCImplementation:
130e0d2066ab8812cbaa16d79af5f2c9fb6bd52d2e2Douglas Gregor    return DC;
1315a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor
1325a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::ObjCProtocol:
1335a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor    if (const ObjCProtocolDecl *Def
1345a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor          = cast<ObjCProtocolDecl>(DC)->getDefinition())
1355a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor      return Def;
1366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
1375a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor
1385a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  // FIXME: These are defined in one place, but properties in class extensions
1395a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  // end up being back-patched into the main interface. See
1405a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  // Sema::HandlePropertyInClassExtension for the offending code.
1415a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  case Decl::ObjCInterface:
1426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
1436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
1445a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  default:
1455a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor    llvm_unreachable("Unhandled DeclContext in AST reader");
1465a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor  }
1475a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor
148e0d2066ab8812cbaa16d79af5f2c9fb6bd52d2e2Douglas Gregor  llvm_unreachable("Unhandled decl kind");
1499cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor}
1509cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor
1519cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregorbool serialization::isRedeclarableDeclKind(unsigned Kind) {
1529cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  switch (static_cast<Decl::Kind>(Kind)) {
1533ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  case Decl::TranslationUnit:
1543ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  case Decl::ExternCContext:
1553ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    // Special case of a "merged" declaration.
1563ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    return true;
1573ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar
1589cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::Namespace:
159176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case Decl::NamespaceAlias:
1609cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::Typedef:
1619cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::TypeAlias:
1629cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::Enum:
1639cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::Record:
1649cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::CXXRecord:
1659cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ClassTemplateSpecialization:
1669cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ClassTemplatePartialSpecialization:
167ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  case Decl::VarTemplateSpecialization:
168ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  case Decl::VarTemplatePartialSpecialization:
1699cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::Function:
1709cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::CXXMethod:
1719cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::CXXConstructor:
1729cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::CXXDestructor:
1739cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::CXXConversion:
174f06a2893bc9778857295c64ee32b4a899a338480Richard Smith  case Decl::UsingShadow:
1759cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::Var:
1769cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::FunctionTemplate:
1779cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ClassTemplate:
178ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  case Decl::VarTemplate:
1799cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::TypeAliasTemplate:
1809cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ObjCProtocol:
1819cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ObjCInterface:
182684aa73192d92850a926870be62a1787eb5b7ed9Michael Han  case Decl::Empty:
1839cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor    return true;
1849cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor
1859cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  // Never redeclarable.
1869cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::UsingDirective:
1879cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::Label:
1889cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::UnresolvedUsingTypename:
1899cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::TemplateTypeParm:
1909cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::EnumConstant:
1919cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::UnresolvedUsingValue:
1929cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::IndirectField:
1939cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::Field:
19476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  case Decl::MSProperty:
1959cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ObjCIvar:
1969cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ObjCAtDefsField:
1979cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::NonTypeTemplateParm:
1989cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::TemplateTemplateParm:
1999cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::Using:
2009cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ObjCMethod:
2019cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ObjCCategory:
2029cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ObjCCategoryImpl:
2039cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ObjCImplementation:
2049cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ObjCProperty:
2059cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ObjCCompatibleAlias:
2069cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::LinkageSpec:
2079cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ObjCPropertyImpl:
2089cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::FileScopeAsm:
2099cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::AccessSpec:
2109cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::Friend:
2119cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::FriendTemplate:
2129cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::StaticAssert:
2139cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::Block:
2146afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  case Decl::Captured:
2159cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::ClassScopeFunctionSpecialization:
2169cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  case Decl::Import:
217c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  case Decl::OMPThreadPrivate:
2189cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor    return false;
219176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
220176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // These indirectly derive from Redeclarable<T> but are not actually
221176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // redeclarable.
222176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case Decl::ImplicitParam:
223176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case Decl::ParmVar:
224176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return false;
2259cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  }
2265a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor
2279cfdc03fe7abab2f413bb7fdc59e9be15c382a74Douglas Gregor  llvm_unreachable("Unhandled declaration kind");
2285a04f9fc2b000da98fd903c8156034304bdadb2fDouglas Gregor}
229176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
230176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesbool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
2310e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // Friend declarations in dependent contexts aren't anonymous in the usual
2320e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // sense, but they cannot be found by name lookup in their semantic context
2330e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // (or indeed in any context), so we treat them as anonymous.
2340e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  //
2350e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // This doesn't apply to friend tag decls; Sema makes those available to name
2360e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // lookup in the surrounding context.
2370e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (D->getFriendObjectKind() &&
2380e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
2390e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // For function templates and class templates, the template is numbered and
2400e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // not its pattern.
2410e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (auto *FD = dyn_cast<FunctionDecl>(D))
2420e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      return !FD->getDescribedFunctionTemplate();
2430e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (auto *RD = dyn_cast<CXXRecordDecl>(D))
2440e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      return !RD->getDescribedClassTemplate();
2450e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return true;
2460e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
2470e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
2480e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // Otherwise, we only care about anonymous class members.
249176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
250176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return false;
251176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  return isa<TagDecl>(D) || isa<FieldDecl>(D);
252176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines}
253176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
254