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/Support/DJB.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: 31 ID = PREDEF_TYPE_VOID_ID; 32 break; 33 case BuiltinType::Bool: 34 ID = PREDEF_TYPE_BOOL_ID; 35 break; 36 case BuiltinType::Char_U: 37 ID = PREDEF_TYPE_CHAR_U_ID; 38 break; 39 case BuiltinType::UChar: 40 ID = PREDEF_TYPE_UCHAR_ID; 41 break; 42 case BuiltinType::UShort: 43 ID = PREDEF_TYPE_USHORT_ID; 44 break; 45 case BuiltinType::UInt: 46 ID = PREDEF_TYPE_UINT_ID; 47 break; 48 case BuiltinType::ULong: 49 ID = PREDEF_TYPE_ULONG_ID; 50 break; 51 case BuiltinType::ULongLong: 52 ID = PREDEF_TYPE_ULONGLONG_ID; 53 break; 54 case BuiltinType::UInt128: 55 ID = PREDEF_TYPE_UINT128_ID; 56 break; 57 case BuiltinType::Char_S: 58 ID = PREDEF_TYPE_CHAR_S_ID; 59 break; 60 case BuiltinType::SChar: 61 ID = PREDEF_TYPE_SCHAR_ID; 62 break; 63 case BuiltinType::WChar_S: 64 case BuiltinType::WChar_U: 65 ID = PREDEF_TYPE_WCHAR_ID; 66 break; 67 case BuiltinType::Short: 68 ID = PREDEF_TYPE_SHORT_ID; 69 break; 70 case BuiltinType::Int: 71 ID = PREDEF_TYPE_INT_ID; 72 break; 73 case BuiltinType::Long: 74 ID = PREDEF_TYPE_LONG_ID; 75 break; 76 case BuiltinType::LongLong: 77 ID = PREDEF_TYPE_LONGLONG_ID; 78 break; 79 case BuiltinType::Int128: 80 ID = PREDEF_TYPE_INT128_ID; 81 break; 82 case BuiltinType::Half: 83 ID = PREDEF_TYPE_HALF_ID; 84 break; 85 case BuiltinType::Float: 86 ID = PREDEF_TYPE_FLOAT_ID; 87 break; 88 case BuiltinType::Double: 89 ID = PREDEF_TYPE_DOUBLE_ID; 90 break; 91 case BuiltinType::LongDouble: 92 ID = PREDEF_TYPE_LONGDOUBLE_ID; 93 break; 94 case BuiltinType::Float16: 95 ID = PREDEF_TYPE_FLOAT16_ID; 96 break; 97 case BuiltinType::Float128: 98 ID = PREDEF_TYPE_FLOAT128_ID; 99 break; 100 case BuiltinType::NullPtr: 101 ID = PREDEF_TYPE_NULLPTR_ID; 102 break; 103 case BuiltinType::Char8: 104 ID = PREDEF_TYPE_CHAR8_ID; 105 break; 106 case BuiltinType::Char16: 107 ID = PREDEF_TYPE_CHAR16_ID; 108 break; 109 case BuiltinType::Char32: 110 ID = PREDEF_TYPE_CHAR32_ID; 111 break; 112 case BuiltinType::Overload: 113 ID = PREDEF_TYPE_OVERLOAD_ID; 114 break; 115 case BuiltinType::BoundMember: 116 ID = PREDEF_TYPE_BOUND_MEMBER; 117 break; 118 case BuiltinType::PseudoObject: 119 ID = PREDEF_TYPE_PSEUDO_OBJECT; 120 break; 121 case BuiltinType::Dependent: 122 ID = PREDEF_TYPE_DEPENDENT_ID; 123 break; 124 case BuiltinType::UnknownAny: 125 ID = PREDEF_TYPE_UNKNOWN_ANY; 126 break; 127 case BuiltinType::ARCUnbridgedCast: 128 ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST; 129 break; 130 case BuiltinType::ObjCId: 131 ID = PREDEF_TYPE_OBJC_ID; 132 break; 133 case BuiltinType::ObjCClass: 134 ID = PREDEF_TYPE_OBJC_CLASS; 135 break; 136 case BuiltinType::ObjCSel: 137 ID = PREDEF_TYPE_OBJC_SEL; 138 break; 139 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 140 case BuiltinType::Id: \ 141 ID = PREDEF_TYPE_##Id##_ID; \ 142 break; 143 #include "clang/Basic/OpenCLImageTypes.def" 144 case BuiltinType::OCLSampler: 145 ID = PREDEF_TYPE_SAMPLER_ID; 146 break; 147 case BuiltinType::OCLEvent: 148 ID = PREDEF_TYPE_EVENT_ID; 149 break; 150 case BuiltinType::OCLClkEvent: 151 ID = PREDEF_TYPE_CLK_EVENT_ID; 152 break; 153 case BuiltinType::OCLQueue: 154 ID = PREDEF_TYPE_QUEUE_ID; 155 break; 156 case BuiltinType::OCLReserveID: 157 ID = PREDEF_TYPE_RESERVE_ID_ID; 158 break; 159 case BuiltinType::BuiltinFn: 160 ID = PREDEF_TYPE_BUILTIN_FN; 161 break; 162 case BuiltinType::OMPArraySection: 163 ID = PREDEF_TYPE_OMP_ARRAY_SECTION; 164 break; 165 } 166 167 return TypeIdx(ID); 168 } 169 170 unsigned serialization::ComputeHash(Selector Sel) { 171 unsigned N = Sel.getNumArgs(); 172 if (N == 0) 173 ++N; 174 unsigned R = 5381; 175 for (unsigned I = 0; I != N; ++I) 176 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I)) 177 R = llvm::djbHash(II->getName(), R); 178 return R; 179 } 180 181 const DeclContext * 182 serialization::getDefinitiveDeclContext(const DeclContext *DC) { 183 switch (DC->getDeclKind()) { 184 // These entities may have multiple definitions. 185 case Decl::TranslationUnit: 186 case Decl::ExternCContext: 187 case Decl::Namespace: 188 case Decl::LinkageSpec: 189 case Decl::Export: 190 return nullptr; 191 192 // C/C++ tag types can only be defined in one place. 193 case Decl::Enum: 194 case Decl::Record: 195 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition()) 196 return Def; 197 return nullptr; 198 199 // FIXME: These can be defined in one place... except special member 200 // functions and out-of-line definitions. 201 case Decl::CXXRecord: 202 case Decl::ClassTemplateSpecialization: 203 case Decl::ClassTemplatePartialSpecialization: 204 return nullptr; 205 206 // Each function, method, and block declaration is its own DeclContext. 207 case Decl::Function: 208 case Decl::CXXMethod: 209 case Decl::CXXConstructor: 210 case Decl::CXXDestructor: 211 case Decl::CXXConversion: 212 case Decl::ObjCMethod: 213 case Decl::Block: 214 case Decl::Captured: 215 // Objective C categories, category implementations, and class 216 // implementations can only be defined in one place. 217 case Decl::ObjCCategory: 218 case Decl::ObjCCategoryImpl: 219 case Decl::ObjCImplementation: 220 return DC; 221 222 case Decl::ObjCProtocol: 223 if (const ObjCProtocolDecl *Def 224 = cast<ObjCProtocolDecl>(DC)->getDefinition()) 225 return Def; 226 return nullptr; 227 228 // FIXME: These are defined in one place, but properties in class extensions 229 // end up being back-patched into the main interface. See 230 // Sema::HandlePropertyInClassExtension for the offending code. 231 case Decl::ObjCInterface: 232 return nullptr; 233 234 default: 235 llvm_unreachable("Unhandled DeclContext in AST reader"); 236 } 237 238 llvm_unreachable("Unhandled decl kind"); 239 } 240 241 bool serialization::isRedeclarableDeclKind(unsigned Kind) { 242 switch (static_cast<Decl::Kind>(Kind)) { 243 case Decl::TranslationUnit: 244 case Decl::ExternCContext: 245 // Special case of a "merged" declaration. 246 return true; 247 248 case Decl::Namespace: 249 case Decl::NamespaceAlias: 250 case Decl::Typedef: 251 case Decl::TypeAlias: 252 case Decl::Enum: 253 case Decl::Record: 254 case Decl::CXXRecord: 255 case Decl::ClassTemplateSpecialization: 256 case Decl::ClassTemplatePartialSpecialization: 257 case Decl::VarTemplateSpecialization: 258 case Decl::VarTemplatePartialSpecialization: 259 case Decl::Function: 260 case Decl::CXXDeductionGuide: 261 case Decl::CXXMethod: 262 case Decl::CXXConstructor: 263 case Decl::CXXDestructor: 264 case Decl::CXXConversion: 265 case Decl::UsingShadow: 266 case Decl::ConstructorUsingShadow: 267 case Decl::Var: 268 case Decl::FunctionTemplate: 269 case Decl::ClassTemplate: 270 case Decl::VarTemplate: 271 case Decl::TypeAliasTemplate: 272 case Decl::ObjCProtocol: 273 case Decl::ObjCInterface: 274 case Decl::Empty: 275 return true; 276 277 // Never redeclarable. 278 case Decl::UsingDirective: 279 case Decl::Label: 280 case Decl::UnresolvedUsingTypename: 281 case Decl::TemplateTypeParm: 282 case Decl::EnumConstant: 283 case Decl::UnresolvedUsingValue: 284 case Decl::IndirectField: 285 case Decl::Field: 286 case Decl::MSProperty: 287 case Decl::ObjCIvar: 288 case Decl::ObjCAtDefsField: 289 case Decl::NonTypeTemplateParm: 290 case Decl::TemplateTemplateParm: 291 case Decl::Using: 292 case Decl::UsingPack: 293 case Decl::ObjCMethod: 294 case Decl::ObjCCategory: 295 case Decl::ObjCCategoryImpl: 296 case Decl::ObjCImplementation: 297 case Decl::ObjCProperty: 298 case Decl::ObjCCompatibleAlias: 299 case Decl::LinkageSpec: 300 case Decl::Export: 301 case Decl::ObjCPropertyImpl: 302 case Decl::PragmaComment: 303 case Decl::PragmaDetectMismatch: 304 case Decl::FileScopeAsm: 305 case Decl::AccessSpec: 306 case Decl::Friend: 307 case Decl::FriendTemplate: 308 case Decl::StaticAssert: 309 case Decl::Block: 310 case Decl::Captured: 311 case Decl::ClassScopeFunctionSpecialization: 312 case Decl::Import: 313 case Decl::OMPThreadPrivate: 314 case Decl::OMPCapturedExpr: 315 case Decl::OMPDeclareReduction: 316 case Decl::BuiltinTemplate: 317 case Decl::Decomposition: 318 case Decl::Binding: 319 return false; 320 321 // These indirectly derive from Redeclarable<T> but are not actually 322 // redeclarable. 323 case Decl::ImplicitParam: 324 case Decl::ParmVar: 325 case Decl::ObjCTypeParam: 326 return false; 327 } 328 329 llvm_unreachable("Unhandled declaration kind"); 330 } 331 332 bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) { 333 // Friend declarations in dependent contexts aren't anonymous in the usual 334 // sense, but they cannot be found by name lookup in their semantic context 335 // (or indeed in any context), so we treat them as anonymous. 336 // 337 // This doesn't apply to friend tag decls; Sema makes those available to name 338 // lookup in the surrounding context. 339 if (D->getFriendObjectKind() && 340 D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) { 341 // For function templates and class templates, the template is numbered and 342 // not its pattern. 343 if (auto *FD = dyn_cast<FunctionDecl>(D)) 344 return !FD->getDescribedFunctionTemplate(); 345 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) 346 return !RD->getDescribedClassTemplate(); 347 return true; 348 } 349 350 // Otherwise, we only care about anonymous class members. 351 if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext())) 352 return false; 353 return isa<TagDecl>(D) || isa<FieldDecl>(D); 354 } 355 356