1f4a2713aSLionel Sambuc //===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===// 2f4a2713aSLionel Sambuc // 3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4f4a2713aSLionel Sambuc // 5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7f4a2713aSLionel Sambuc // 8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9f4a2713aSLionel Sambuc // 10f4a2713aSLionel Sambuc // This file defines common functions that both ASTReader and ASTWriter use. 11f4a2713aSLionel Sambuc // 12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 13f4a2713aSLionel Sambuc 14f4a2713aSLionel Sambuc #include "ASTCommon.h" 15*0a6a1f1dSLionel Sambuc #include "clang/AST/DeclCXX.h" 16f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h" 17f4a2713aSLionel Sambuc #include "clang/Basic/IdentifierTable.h" 18f4a2713aSLionel Sambuc #include "clang/Serialization/ASTDeserializationListener.h" 19f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h" 20f4a2713aSLionel Sambuc 21f4a2713aSLionel Sambuc using namespace clang; 22f4a2713aSLionel Sambuc 23f4a2713aSLionel Sambuc // Give ASTDeserializationListener's VTable a home. ~ASTDeserializationListener()24f4a2713aSLionel SambucASTDeserializationListener::~ASTDeserializationListener() { } 25f4a2713aSLionel Sambuc 26f4a2713aSLionel Sambuc serialization::TypeIdx TypeIdxFromBuiltin(const BuiltinType * BT)27f4a2713aSLionel Sambucserialization::TypeIdxFromBuiltin(const BuiltinType *BT) { 28f4a2713aSLionel Sambuc unsigned ID = 0; 29f4a2713aSLionel Sambuc switch (BT->getKind()) { 30f4a2713aSLionel Sambuc case BuiltinType::Void: ID = PREDEF_TYPE_VOID_ID; break; 31f4a2713aSLionel Sambuc case BuiltinType::Bool: ID = PREDEF_TYPE_BOOL_ID; break; 32f4a2713aSLionel Sambuc case BuiltinType::Char_U: ID = PREDEF_TYPE_CHAR_U_ID; break; 33f4a2713aSLionel Sambuc case BuiltinType::UChar: ID = PREDEF_TYPE_UCHAR_ID; break; 34f4a2713aSLionel Sambuc case BuiltinType::UShort: ID = PREDEF_TYPE_USHORT_ID; break; 35f4a2713aSLionel Sambuc case BuiltinType::UInt: ID = PREDEF_TYPE_UINT_ID; break; 36f4a2713aSLionel Sambuc case BuiltinType::ULong: ID = PREDEF_TYPE_ULONG_ID; break; 37f4a2713aSLionel Sambuc case BuiltinType::ULongLong: ID = PREDEF_TYPE_ULONGLONG_ID; break; 38f4a2713aSLionel Sambuc case BuiltinType::UInt128: ID = PREDEF_TYPE_UINT128_ID; break; 39f4a2713aSLionel Sambuc case BuiltinType::Char_S: ID = PREDEF_TYPE_CHAR_S_ID; break; 40f4a2713aSLionel Sambuc case BuiltinType::SChar: ID = PREDEF_TYPE_SCHAR_ID; break; 41f4a2713aSLionel Sambuc case BuiltinType::WChar_S: 42f4a2713aSLionel Sambuc case BuiltinType::WChar_U: ID = PREDEF_TYPE_WCHAR_ID; break; 43f4a2713aSLionel Sambuc case BuiltinType::Short: ID = PREDEF_TYPE_SHORT_ID; break; 44f4a2713aSLionel Sambuc case BuiltinType::Int: ID = PREDEF_TYPE_INT_ID; break; 45f4a2713aSLionel Sambuc case BuiltinType::Long: ID = PREDEF_TYPE_LONG_ID; break; 46f4a2713aSLionel Sambuc case BuiltinType::LongLong: ID = PREDEF_TYPE_LONGLONG_ID; break; 47f4a2713aSLionel Sambuc case BuiltinType::Int128: ID = PREDEF_TYPE_INT128_ID; break; 48f4a2713aSLionel Sambuc case BuiltinType::Half: ID = PREDEF_TYPE_HALF_ID; break; 49f4a2713aSLionel Sambuc case BuiltinType::Float: ID = PREDEF_TYPE_FLOAT_ID; break; 50f4a2713aSLionel Sambuc case BuiltinType::Double: ID = PREDEF_TYPE_DOUBLE_ID; break; 51f4a2713aSLionel Sambuc case BuiltinType::LongDouble: ID = PREDEF_TYPE_LONGDOUBLE_ID; break; 52f4a2713aSLionel Sambuc case BuiltinType::NullPtr: ID = PREDEF_TYPE_NULLPTR_ID; break; 53f4a2713aSLionel Sambuc case BuiltinType::Char16: ID = PREDEF_TYPE_CHAR16_ID; break; 54f4a2713aSLionel Sambuc case BuiltinType::Char32: ID = PREDEF_TYPE_CHAR32_ID; break; 55f4a2713aSLionel Sambuc case BuiltinType::Overload: ID = PREDEF_TYPE_OVERLOAD_ID; break; 56f4a2713aSLionel Sambuc case BuiltinType::BoundMember:ID = PREDEF_TYPE_BOUND_MEMBER; break; 57f4a2713aSLionel Sambuc case BuiltinType::PseudoObject:ID = PREDEF_TYPE_PSEUDO_OBJECT;break; 58f4a2713aSLionel Sambuc case BuiltinType::Dependent: ID = PREDEF_TYPE_DEPENDENT_ID; break; 59f4a2713aSLionel Sambuc case BuiltinType::UnknownAny: ID = PREDEF_TYPE_UNKNOWN_ANY; break; 60f4a2713aSLionel Sambuc case BuiltinType::ARCUnbridgedCast: 61f4a2713aSLionel Sambuc ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST; break; 62f4a2713aSLionel Sambuc case BuiltinType::ObjCId: ID = PREDEF_TYPE_OBJC_ID; break; 63f4a2713aSLionel Sambuc case BuiltinType::ObjCClass: ID = PREDEF_TYPE_OBJC_CLASS; break; 64f4a2713aSLionel Sambuc case BuiltinType::ObjCSel: ID = PREDEF_TYPE_OBJC_SEL; break; 65f4a2713aSLionel Sambuc case BuiltinType::OCLImage1d: ID = PREDEF_TYPE_IMAGE1D_ID; break; 66f4a2713aSLionel Sambuc case BuiltinType::OCLImage1dArray: ID = PREDEF_TYPE_IMAGE1D_ARR_ID; break; 67f4a2713aSLionel Sambuc case BuiltinType::OCLImage1dBuffer: ID = PREDEF_TYPE_IMAGE1D_BUFF_ID; break; 68f4a2713aSLionel Sambuc case BuiltinType::OCLImage2d: ID = PREDEF_TYPE_IMAGE2D_ID; break; 69f4a2713aSLionel Sambuc case BuiltinType::OCLImage2dArray: ID = PREDEF_TYPE_IMAGE2D_ARR_ID; break; 70f4a2713aSLionel Sambuc case BuiltinType::OCLImage3d: ID = PREDEF_TYPE_IMAGE3D_ID; break; 71f4a2713aSLionel Sambuc case BuiltinType::OCLSampler: ID = PREDEF_TYPE_SAMPLER_ID; break; 72f4a2713aSLionel Sambuc case BuiltinType::OCLEvent: ID = PREDEF_TYPE_EVENT_ID; break; 73f4a2713aSLionel Sambuc case BuiltinType::BuiltinFn: 74f4a2713aSLionel Sambuc ID = PREDEF_TYPE_BUILTIN_FN; break; 75f4a2713aSLionel Sambuc 76f4a2713aSLionel Sambuc } 77f4a2713aSLionel Sambuc 78f4a2713aSLionel Sambuc return TypeIdx(ID); 79f4a2713aSLionel Sambuc } 80f4a2713aSLionel Sambuc ComputeHash(Selector Sel)81f4a2713aSLionel Sambucunsigned serialization::ComputeHash(Selector Sel) { 82f4a2713aSLionel Sambuc unsigned N = Sel.getNumArgs(); 83f4a2713aSLionel Sambuc if (N == 0) 84f4a2713aSLionel Sambuc ++N; 85f4a2713aSLionel Sambuc unsigned R = 5381; 86f4a2713aSLionel Sambuc for (unsigned I = 0; I != N; ++I) 87f4a2713aSLionel Sambuc if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I)) 88f4a2713aSLionel Sambuc R = llvm::HashString(II->getName(), R); 89f4a2713aSLionel Sambuc return R; 90f4a2713aSLionel Sambuc } 91f4a2713aSLionel Sambuc 92f4a2713aSLionel Sambuc const DeclContext * getDefinitiveDeclContext(const DeclContext * DC)93f4a2713aSLionel Sambucserialization::getDefinitiveDeclContext(const DeclContext *DC) { 94f4a2713aSLionel Sambuc switch (DC->getDeclKind()) { 95f4a2713aSLionel Sambuc // These entities may have multiple definitions. 96f4a2713aSLionel Sambuc case Decl::TranslationUnit: 97f4a2713aSLionel Sambuc case Decl::Namespace: 98f4a2713aSLionel Sambuc case Decl::LinkageSpec: 99*0a6a1f1dSLionel Sambuc return nullptr; 100f4a2713aSLionel Sambuc 101f4a2713aSLionel Sambuc // C/C++ tag types can only be defined in one place. 102f4a2713aSLionel Sambuc case Decl::Enum: 103f4a2713aSLionel Sambuc case Decl::Record: 104f4a2713aSLionel Sambuc if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition()) 105f4a2713aSLionel Sambuc return Def; 106*0a6a1f1dSLionel Sambuc return nullptr; 107f4a2713aSLionel Sambuc 108f4a2713aSLionel Sambuc // FIXME: These can be defined in one place... except special member 109f4a2713aSLionel Sambuc // functions and out-of-line definitions. 110f4a2713aSLionel Sambuc case Decl::CXXRecord: 111f4a2713aSLionel Sambuc case Decl::ClassTemplateSpecialization: 112f4a2713aSLionel Sambuc case Decl::ClassTemplatePartialSpecialization: 113*0a6a1f1dSLionel Sambuc return nullptr; 114f4a2713aSLionel Sambuc 115f4a2713aSLionel Sambuc // Each function, method, and block declaration is its own DeclContext. 116f4a2713aSLionel Sambuc case Decl::Function: 117f4a2713aSLionel Sambuc case Decl::CXXMethod: 118f4a2713aSLionel Sambuc case Decl::CXXConstructor: 119f4a2713aSLionel Sambuc case Decl::CXXDestructor: 120f4a2713aSLionel Sambuc case Decl::CXXConversion: 121f4a2713aSLionel Sambuc case Decl::ObjCMethod: 122f4a2713aSLionel Sambuc case Decl::Block: 123f4a2713aSLionel Sambuc case Decl::Captured: 124f4a2713aSLionel Sambuc // Objective C categories, category implementations, and class 125f4a2713aSLionel Sambuc // implementations can only be defined in one place. 126f4a2713aSLionel Sambuc case Decl::ObjCCategory: 127f4a2713aSLionel Sambuc case Decl::ObjCCategoryImpl: 128f4a2713aSLionel Sambuc case Decl::ObjCImplementation: 129f4a2713aSLionel Sambuc return DC; 130f4a2713aSLionel Sambuc 131f4a2713aSLionel Sambuc case Decl::ObjCProtocol: 132f4a2713aSLionel Sambuc if (const ObjCProtocolDecl *Def 133f4a2713aSLionel Sambuc = cast<ObjCProtocolDecl>(DC)->getDefinition()) 134f4a2713aSLionel Sambuc return Def; 135*0a6a1f1dSLionel Sambuc return nullptr; 136f4a2713aSLionel Sambuc 137f4a2713aSLionel Sambuc // FIXME: These are defined in one place, but properties in class extensions 138f4a2713aSLionel Sambuc // end up being back-patched into the main interface. See 139f4a2713aSLionel Sambuc // Sema::HandlePropertyInClassExtension for the offending code. 140f4a2713aSLionel Sambuc case Decl::ObjCInterface: 141*0a6a1f1dSLionel Sambuc return nullptr; 142f4a2713aSLionel Sambuc 143f4a2713aSLionel Sambuc default: 144f4a2713aSLionel Sambuc llvm_unreachable("Unhandled DeclContext in AST reader"); 145f4a2713aSLionel Sambuc } 146f4a2713aSLionel Sambuc 147f4a2713aSLionel Sambuc llvm_unreachable("Unhandled decl kind"); 148f4a2713aSLionel Sambuc } 149f4a2713aSLionel Sambuc isRedeclarableDeclKind(unsigned Kind)150f4a2713aSLionel Sambucbool serialization::isRedeclarableDeclKind(unsigned Kind) { 151f4a2713aSLionel Sambuc switch (static_cast<Decl::Kind>(Kind)) { 152f4a2713aSLionel Sambuc case Decl::TranslationUnit: // Special case of a "merged" declaration. 153f4a2713aSLionel Sambuc case Decl::Namespace: 154*0a6a1f1dSLionel Sambuc case Decl::NamespaceAlias: 155f4a2713aSLionel Sambuc case Decl::Typedef: 156f4a2713aSLionel Sambuc case Decl::TypeAlias: 157f4a2713aSLionel Sambuc case Decl::Enum: 158f4a2713aSLionel Sambuc case Decl::Record: 159f4a2713aSLionel Sambuc case Decl::CXXRecord: 160f4a2713aSLionel Sambuc case Decl::ClassTemplateSpecialization: 161f4a2713aSLionel Sambuc case Decl::ClassTemplatePartialSpecialization: 162f4a2713aSLionel Sambuc case Decl::VarTemplateSpecialization: 163f4a2713aSLionel Sambuc case Decl::VarTemplatePartialSpecialization: 164f4a2713aSLionel Sambuc case Decl::Function: 165f4a2713aSLionel Sambuc case Decl::CXXMethod: 166f4a2713aSLionel Sambuc case Decl::CXXConstructor: 167f4a2713aSLionel Sambuc case Decl::CXXDestructor: 168f4a2713aSLionel Sambuc case Decl::CXXConversion: 169f4a2713aSLionel Sambuc case Decl::UsingShadow: 170f4a2713aSLionel Sambuc case Decl::Var: 171f4a2713aSLionel Sambuc case Decl::FunctionTemplate: 172f4a2713aSLionel Sambuc case Decl::ClassTemplate: 173f4a2713aSLionel Sambuc case Decl::VarTemplate: 174f4a2713aSLionel Sambuc case Decl::TypeAliasTemplate: 175f4a2713aSLionel Sambuc case Decl::ObjCProtocol: 176f4a2713aSLionel Sambuc case Decl::ObjCInterface: 177f4a2713aSLionel Sambuc case Decl::Empty: 178f4a2713aSLionel Sambuc return true; 179f4a2713aSLionel Sambuc 180f4a2713aSLionel Sambuc // Never redeclarable. 181f4a2713aSLionel Sambuc case Decl::UsingDirective: 182f4a2713aSLionel Sambuc case Decl::Label: 183f4a2713aSLionel Sambuc case Decl::UnresolvedUsingTypename: 184f4a2713aSLionel Sambuc case Decl::TemplateTypeParm: 185f4a2713aSLionel Sambuc case Decl::EnumConstant: 186f4a2713aSLionel Sambuc case Decl::UnresolvedUsingValue: 187f4a2713aSLionel Sambuc case Decl::IndirectField: 188f4a2713aSLionel Sambuc case Decl::Field: 189f4a2713aSLionel Sambuc case Decl::MSProperty: 190f4a2713aSLionel Sambuc case Decl::ObjCIvar: 191f4a2713aSLionel Sambuc case Decl::ObjCAtDefsField: 192f4a2713aSLionel Sambuc case Decl::NonTypeTemplateParm: 193f4a2713aSLionel Sambuc case Decl::TemplateTemplateParm: 194f4a2713aSLionel Sambuc case Decl::Using: 195f4a2713aSLionel Sambuc case Decl::ObjCMethod: 196f4a2713aSLionel Sambuc case Decl::ObjCCategory: 197f4a2713aSLionel Sambuc case Decl::ObjCCategoryImpl: 198f4a2713aSLionel Sambuc case Decl::ObjCImplementation: 199f4a2713aSLionel Sambuc case Decl::ObjCProperty: 200f4a2713aSLionel Sambuc case Decl::ObjCCompatibleAlias: 201f4a2713aSLionel Sambuc case Decl::LinkageSpec: 202f4a2713aSLionel Sambuc case Decl::ObjCPropertyImpl: 203f4a2713aSLionel Sambuc case Decl::FileScopeAsm: 204f4a2713aSLionel Sambuc case Decl::AccessSpec: 205f4a2713aSLionel Sambuc case Decl::Friend: 206f4a2713aSLionel Sambuc case Decl::FriendTemplate: 207f4a2713aSLionel Sambuc case Decl::StaticAssert: 208f4a2713aSLionel Sambuc case Decl::Block: 209f4a2713aSLionel Sambuc case Decl::Captured: 210f4a2713aSLionel Sambuc case Decl::ClassScopeFunctionSpecialization: 211f4a2713aSLionel Sambuc case Decl::Import: 212f4a2713aSLionel Sambuc case Decl::OMPThreadPrivate: 213f4a2713aSLionel Sambuc return false; 214*0a6a1f1dSLionel Sambuc 215*0a6a1f1dSLionel Sambuc // These indirectly derive from Redeclarable<T> but are not actually 216*0a6a1f1dSLionel Sambuc // redeclarable. 217*0a6a1f1dSLionel Sambuc case Decl::ImplicitParam: 218*0a6a1f1dSLionel Sambuc case Decl::ParmVar: 219*0a6a1f1dSLionel Sambuc return false; 220f4a2713aSLionel Sambuc } 221f4a2713aSLionel Sambuc 222f4a2713aSLionel Sambuc llvm_unreachable("Unhandled declaration kind"); 223f4a2713aSLionel Sambuc } 224*0a6a1f1dSLionel Sambuc needsAnonymousDeclarationNumber(const NamedDecl * D)225*0a6a1f1dSLionel Sambucbool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) { 226*0a6a1f1dSLionel Sambuc if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext())) 227*0a6a1f1dSLionel Sambuc return false; 228*0a6a1f1dSLionel Sambuc return isa<TagDecl>(D) || isa<FieldDecl>(D); 229*0a6a1f1dSLionel Sambuc } 230*0a6a1f1dSLionel Sambuc 231