ASTCommon.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. //===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines common functions that both ASTReader and ASTWriter use.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ASTCommon.h"
  13. #include "clang/AST/DeclCXX.h"
  14. #include "clang/AST/DeclObjC.h"
  15. #include "clang/Basic/IdentifierTable.h"
  16. #include "clang/Serialization/ASTDeserializationListener.h"
  17. #include "llvm/Support/DJB.h"
  18. using namespace clang;
  19. // Give ASTDeserializationListener's VTable a home.
  20. ASTDeserializationListener::~ASTDeserializationListener() { }
  21. serialization::TypeIdx
  22. serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
  23. unsigned ID = 0;
  24. switch (BT->getKind()) {
  25. case BuiltinType::Void:
  26. ID = PREDEF_TYPE_VOID_ID;
  27. break;
  28. case BuiltinType::Bool:
  29. ID = PREDEF_TYPE_BOOL_ID;
  30. break;
  31. case BuiltinType::Char_U:
  32. ID = PREDEF_TYPE_CHAR_U_ID;
  33. break;
  34. case BuiltinType::UChar:
  35. ID = PREDEF_TYPE_UCHAR_ID;
  36. break;
  37. case BuiltinType::UShort:
  38. ID = PREDEF_TYPE_USHORT_ID;
  39. break;
  40. case BuiltinType::UInt:
  41. ID = PREDEF_TYPE_UINT_ID;
  42. break;
  43. case BuiltinType::ULong:
  44. ID = PREDEF_TYPE_ULONG_ID;
  45. break;
  46. case BuiltinType::ULongLong:
  47. ID = PREDEF_TYPE_ULONGLONG_ID;
  48. break;
  49. case BuiltinType::UInt128:
  50. ID = PREDEF_TYPE_UINT128_ID;
  51. break;
  52. case BuiltinType::Char_S:
  53. ID = PREDEF_TYPE_CHAR_S_ID;
  54. break;
  55. case BuiltinType::SChar:
  56. ID = PREDEF_TYPE_SCHAR_ID;
  57. break;
  58. case BuiltinType::WChar_S:
  59. case BuiltinType::WChar_U:
  60. ID = PREDEF_TYPE_WCHAR_ID;
  61. break;
  62. case BuiltinType::Short:
  63. ID = PREDEF_TYPE_SHORT_ID;
  64. break;
  65. case BuiltinType::Int:
  66. ID = PREDEF_TYPE_INT_ID;
  67. break;
  68. case BuiltinType::Long:
  69. ID = PREDEF_TYPE_LONG_ID;
  70. break;
  71. case BuiltinType::LongLong:
  72. ID = PREDEF_TYPE_LONGLONG_ID;
  73. break;
  74. case BuiltinType::Int128:
  75. ID = PREDEF_TYPE_INT128_ID;
  76. break;
  77. case BuiltinType::Half:
  78. ID = PREDEF_TYPE_HALF_ID;
  79. break;
  80. case BuiltinType::Float:
  81. ID = PREDEF_TYPE_FLOAT_ID;
  82. break;
  83. case BuiltinType::Double:
  84. ID = PREDEF_TYPE_DOUBLE_ID;
  85. break;
  86. case BuiltinType::LongDouble:
  87. ID = PREDEF_TYPE_LONGDOUBLE_ID;
  88. break;
  89. case BuiltinType::ShortAccum:
  90. ID = PREDEF_TYPE_SHORT_ACCUM_ID;
  91. break;
  92. case BuiltinType::Accum:
  93. ID = PREDEF_TYPE_ACCUM_ID;
  94. break;
  95. case BuiltinType::LongAccum:
  96. ID = PREDEF_TYPE_LONG_ACCUM_ID;
  97. break;
  98. case BuiltinType::UShortAccum:
  99. ID = PREDEF_TYPE_USHORT_ACCUM_ID;
  100. break;
  101. case BuiltinType::UAccum:
  102. ID = PREDEF_TYPE_UACCUM_ID;
  103. break;
  104. case BuiltinType::ULongAccum:
  105. ID = PREDEF_TYPE_ULONG_ACCUM_ID;
  106. break;
  107. case BuiltinType::ShortFract:
  108. ID = PREDEF_TYPE_SHORT_FRACT_ID;
  109. break;
  110. case BuiltinType::Fract:
  111. ID = PREDEF_TYPE_FRACT_ID;
  112. break;
  113. case BuiltinType::LongFract:
  114. ID = PREDEF_TYPE_LONG_FRACT_ID;
  115. break;
  116. case BuiltinType::UShortFract:
  117. ID = PREDEF_TYPE_USHORT_FRACT_ID;
  118. break;
  119. case BuiltinType::UFract:
  120. ID = PREDEF_TYPE_UFRACT_ID;
  121. break;
  122. case BuiltinType::ULongFract:
  123. ID = PREDEF_TYPE_ULONG_FRACT_ID;
  124. break;
  125. case BuiltinType::SatShortAccum:
  126. ID = PREDEF_TYPE_SAT_SHORT_ACCUM_ID;
  127. break;
  128. case BuiltinType::SatAccum:
  129. ID = PREDEF_TYPE_SAT_ACCUM_ID;
  130. break;
  131. case BuiltinType::SatLongAccum:
  132. ID = PREDEF_TYPE_SAT_LONG_ACCUM_ID;
  133. break;
  134. case BuiltinType::SatUShortAccum:
  135. ID = PREDEF_TYPE_SAT_USHORT_ACCUM_ID;
  136. break;
  137. case BuiltinType::SatUAccum:
  138. ID = PREDEF_TYPE_SAT_UACCUM_ID;
  139. break;
  140. case BuiltinType::SatULongAccum:
  141. ID = PREDEF_TYPE_SAT_ULONG_ACCUM_ID;
  142. break;
  143. case BuiltinType::SatShortFract:
  144. ID = PREDEF_TYPE_SAT_SHORT_FRACT_ID;
  145. break;
  146. case BuiltinType::SatFract:
  147. ID = PREDEF_TYPE_SAT_FRACT_ID;
  148. break;
  149. case BuiltinType::SatLongFract:
  150. ID = PREDEF_TYPE_SAT_LONG_FRACT_ID;
  151. break;
  152. case BuiltinType::SatUShortFract:
  153. ID = PREDEF_TYPE_SAT_USHORT_FRACT_ID;
  154. break;
  155. case BuiltinType::SatUFract:
  156. ID = PREDEF_TYPE_SAT_UFRACT_ID;
  157. break;
  158. case BuiltinType::SatULongFract:
  159. ID = PREDEF_TYPE_SAT_ULONG_FRACT_ID;
  160. break;
  161. case BuiltinType::Float16:
  162. ID = PREDEF_TYPE_FLOAT16_ID;
  163. break;
  164. case BuiltinType::Float128:
  165. ID = PREDEF_TYPE_FLOAT128_ID;
  166. break;
  167. case BuiltinType::Ibm128:
  168. ID = PREDEF_TYPE_IBM128_ID;
  169. break;
  170. case BuiltinType::NullPtr:
  171. ID = PREDEF_TYPE_NULLPTR_ID;
  172. break;
  173. case BuiltinType::Char8:
  174. ID = PREDEF_TYPE_CHAR8_ID;
  175. break;
  176. case BuiltinType::Char16:
  177. ID = PREDEF_TYPE_CHAR16_ID;
  178. break;
  179. case BuiltinType::Char32:
  180. ID = PREDEF_TYPE_CHAR32_ID;
  181. break;
  182. case BuiltinType::Overload:
  183. ID = PREDEF_TYPE_OVERLOAD_ID;
  184. break;
  185. case BuiltinType::BoundMember:
  186. ID = PREDEF_TYPE_BOUND_MEMBER;
  187. break;
  188. case BuiltinType::PseudoObject:
  189. ID = PREDEF_TYPE_PSEUDO_OBJECT;
  190. break;
  191. case BuiltinType::Dependent:
  192. ID = PREDEF_TYPE_DEPENDENT_ID;
  193. break;
  194. case BuiltinType::UnknownAny:
  195. ID = PREDEF_TYPE_UNKNOWN_ANY;
  196. break;
  197. case BuiltinType::ARCUnbridgedCast:
  198. ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;
  199. break;
  200. case BuiltinType::ObjCId:
  201. ID = PREDEF_TYPE_OBJC_ID;
  202. break;
  203. case BuiltinType::ObjCClass:
  204. ID = PREDEF_TYPE_OBJC_CLASS;
  205. break;
  206. case BuiltinType::ObjCSel:
  207. ID = PREDEF_TYPE_OBJC_SEL;
  208. break;
  209. #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
  210. case BuiltinType::Id: \
  211. ID = PREDEF_TYPE_##Id##_ID; \
  212. break;
  213. #include "clang/Basic/OpenCLImageTypes.def"
  214. #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
  215. case BuiltinType::Id: \
  216. ID = PREDEF_TYPE_##Id##_ID; \
  217. break;
  218. #include "clang/Basic/OpenCLExtensionTypes.def"
  219. case BuiltinType::OCLSampler:
  220. ID = PREDEF_TYPE_SAMPLER_ID;
  221. break;
  222. case BuiltinType::OCLEvent:
  223. ID = PREDEF_TYPE_EVENT_ID;
  224. break;
  225. case BuiltinType::OCLClkEvent:
  226. ID = PREDEF_TYPE_CLK_EVENT_ID;
  227. break;
  228. case BuiltinType::OCLQueue:
  229. ID = PREDEF_TYPE_QUEUE_ID;
  230. break;
  231. case BuiltinType::OCLReserveID:
  232. ID = PREDEF_TYPE_RESERVE_ID_ID;
  233. break;
  234. #define SVE_TYPE(Name, Id, SingletonId) \
  235. case BuiltinType::Id: \
  236. ID = PREDEF_TYPE_##Id##_ID; \
  237. break;
  238. #include "clang/Basic/AArch64SVEACLETypes.def"
  239. #define PPC_VECTOR_TYPE(Name, Id, Size) \
  240. case BuiltinType::Id: \
  241. ID = PREDEF_TYPE_##Id##_ID; \
  242. break;
  243. #include "clang/Basic/PPCTypes.def"
  244. #define RVV_TYPE(Name, Id, SingletonId) \
  245. case BuiltinType::Id: \
  246. ID = PREDEF_TYPE_##Id##_ID; \
  247. break;
  248. #include "clang/Basic/RISCVVTypes.def"
  249. case BuiltinType::BuiltinFn:
  250. ID = PREDEF_TYPE_BUILTIN_FN;
  251. break;
  252. case BuiltinType::IncompleteMatrixIdx:
  253. ID = PREDEF_TYPE_INCOMPLETE_MATRIX_IDX;
  254. break;
  255. case BuiltinType::OMPArraySection:
  256. ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
  257. break;
  258. case BuiltinType::OMPArrayShaping:
  259. ID = PREDEF_TYPE_OMP_ARRAY_SHAPING;
  260. break;
  261. case BuiltinType::OMPIterator:
  262. ID = PREDEF_TYPE_OMP_ITERATOR;
  263. break;
  264. case BuiltinType::BFloat16:
  265. ID = PREDEF_TYPE_BFLOAT16_ID;
  266. break;
  267. }
  268. return TypeIdx(ID);
  269. }
  270. unsigned serialization::ComputeHash(Selector Sel) {
  271. unsigned N = Sel.getNumArgs();
  272. if (N == 0)
  273. ++N;
  274. unsigned R = 5381;
  275. for (unsigned I = 0; I != N; ++I)
  276. if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
  277. R = llvm::djbHash(II->getName(), R);
  278. return R;
  279. }
  280. const DeclContext *
  281. serialization::getDefinitiveDeclContext(const DeclContext *DC) {
  282. switch (DC->getDeclKind()) {
  283. // These entities may have multiple definitions.
  284. case Decl::TranslationUnit:
  285. case Decl::ExternCContext:
  286. case Decl::Namespace:
  287. case Decl::LinkageSpec:
  288. case Decl::Export:
  289. return nullptr;
  290. // C/C++ tag types can only be defined in one place.
  291. case Decl::Enum:
  292. case Decl::Record:
  293. if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
  294. return Def;
  295. return nullptr;
  296. // FIXME: These can be defined in one place... except special member
  297. // functions and out-of-line definitions.
  298. case Decl::CXXRecord:
  299. case Decl::ClassTemplateSpecialization:
  300. case Decl::ClassTemplatePartialSpecialization:
  301. return nullptr;
  302. // Each function, method, and block declaration is its own DeclContext.
  303. case Decl::Function:
  304. case Decl::CXXMethod:
  305. case Decl::CXXConstructor:
  306. case Decl::CXXDestructor:
  307. case Decl::CXXConversion:
  308. case Decl::ObjCMethod:
  309. case Decl::Block:
  310. case Decl::Captured:
  311. // Objective C categories, category implementations, and class
  312. // implementations can only be defined in one place.
  313. case Decl::ObjCCategory:
  314. case Decl::ObjCCategoryImpl:
  315. case Decl::ObjCImplementation:
  316. return DC;
  317. case Decl::ObjCProtocol:
  318. if (const ObjCProtocolDecl *Def
  319. = cast<ObjCProtocolDecl>(DC)->getDefinition())
  320. return Def;
  321. return nullptr;
  322. // FIXME: These are defined in one place, but properties in class extensions
  323. // end up being back-patched into the main interface. See
  324. // Sema::HandlePropertyInClassExtension for the offending code.
  325. case Decl::ObjCInterface:
  326. return nullptr;
  327. default:
  328. llvm_unreachable("Unhandled DeclContext in AST reader");
  329. }
  330. llvm_unreachable("Unhandled decl kind");
  331. }
  332. bool serialization::isRedeclarableDeclKind(unsigned Kind) {
  333. switch (static_cast<Decl::Kind>(Kind)) {
  334. case Decl::TranslationUnit:
  335. case Decl::ExternCContext:
  336. // Special case of a "merged" declaration.
  337. return true;
  338. case Decl::Namespace:
  339. case Decl::NamespaceAlias:
  340. case Decl::Typedef:
  341. case Decl::TypeAlias:
  342. case Decl::Enum:
  343. case Decl::Record:
  344. case Decl::CXXRecord:
  345. case Decl::ClassTemplateSpecialization:
  346. case Decl::ClassTemplatePartialSpecialization:
  347. case Decl::VarTemplateSpecialization:
  348. case Decl::VarTemplatePartialSpecialization:
  349. case Decl::Function:
  350. case Decl::CXXDeductionGuide:
  351. case Decl::CXXMethod:
  352. case Decl::CXXConstructor:
  353. case Decl::CXXDestructor:
  354. case Decl::CXXConversion:
  355. case Decl::UsingShadow:
  356. case Decl::ConstructorUsingShadow:
  357. case Decl::Var:
  358. case Decl::FunctionTemplate:
  359. case Decl::ClassTemplate:
  360. case Decl::VarTemplate:
  361. case Decl::TypeAliasTemplate:
  362. case Decl::ObjCProtocol:
  363. case Decl::ObjCInterface:
  364. case Decl::Empty:
  365. return true;
  366. // Never redeclarable.
  367. case Decl::UsingDirective:
  368. case Decl::Label:
  369. case Decl::UnresolvedUsingTypename:
  370. case Decl::TemplateTypeParm:
  371. case Decl::EnumConstant:
  372. case Decl::UnresolvedUsingValue:
  373. case Decl::IndirectField:
  374. case Decl::Field:
  375. case Decl::MSProperty:
  376. case Decl::MSGuid:
  377. case Decl::UnnamedGlobalConstant:
  378. case Decl::TemplateParamObject:
  379. case Decl::ObjCIvar:
  380. case Decl::ObjCAtDefsField:
  381. case Decl::NonTypeTemplateParm:
  382. case Decl::TemplateTemplateParm:
  383. case Decl::Using:
  384. case Decl::UsingEnum:
  385. case Decl::UsingPack:
  386. case Decl::ObjCMethod:
  387. case Decl::ObjCCategory:
  388. case Decl::ObjCCategoryImpl:
  389. case Decl::ObjCImplementation:
  390. case Decl::ObjCProperty:
  391. case Decl::ObjCCompatibleAlias:
  392. case Decl::LinkageSpec:
  393. case Decl::Export:
  394. case Decl::ObjCPropertyImpl:
  395. case Decl::PragmaComment:
  396. case Decl::PragmaDetectMismatch:
  397. case Decl::FileScopeAsm:
  398. case Decl::TopLevelStmt:
  399. case Decl::AccessSpec:
  400. case Decl::Friend:
  401. case Decl::FriendTemplate:
  402. case Decl::StaticAssert:
  403. case Decl::Block:
  404. case Decl::Captured:
  405. case Decl::ClassScopeFunctionSpecialization:
  406. case Decl::Import:
  407. case Decl::OMPThreadPrivate:
  408. case Decl::OMPAllocate:
  409. case Decl::OMPRequires:
  410. case Decl::OMPCapturedExpr:
  411. case Decl::OMPDeclareReduction:
  412. case Decl::OMPDeclareMapper:
  413. case Decl::BuiltinTemplate:
  414. case Decl::Decomposition:
  415. case Decl::Binding:
  416. case Decl::Concept:
  417. case Decl::ImplicitConceptSpecialization:
  418. case Decl::LifetimeExtendedTemporary:
  419. case Decl::RequiresExprBody:
  420. case Decl::UnresolvedUsingIfExists:
  421. case Decl::HLSLBuffer:
  422. return false;
  423. // These indirectly derive from Redeclarable<T> but are not actually
  424. // redeclarable.
  425. case Decl::ImplicitParam:
  426. case Decl::ParmVar:
  427. case Decl::ObjCTypeParam:
  428. return false;
  429. }
  430. llvm_unreachable("Unhandled declaration kind");
  431. }
  432. bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
  433. // Friend declarations in dependent contexts aren't anonymous in the usual
  434. // sense, but they cannot be found by name lookup in their semantic context
  435. // (or indeed in any context), so we treat them as anonymous.
  436. //
  437. // This doesn't apply to friend tag decls; Sema makes those available to name
  438. // lookup in the surrounding context.
  439. if (D->getFriendObjectKind() &&
  440. D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
  441. // For function templates and class templates, the template is numbered and
  442. // not its pattern.
  443. if (auto *FD = dyn_cast<FunctionDecl>(D))
  444. return !FD->getDescribedFunctionTemplate();
  445. if (auto *RD = dyn_cast<CXXRecordDecl>(D))
  446. return !RD->getDescribedClassTemplate();
  447. return true;
  448. }
  449. // At block scope, we number everything that we need to deduplicate, since we
  450. // can't just use name matching to keep things lined up.
  451. // FIXME: This is only necessary for an inline function or a template or
  452. // similar.
  453. if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
  454. if (auto *VD = dyn_cast<VarDecl>(D))
  455. return VD->isStaticLocal();
  456. // FIXME: What about CapturedDecls (and declarations nested within them)?
  457. return isa<TagDecl, BlockDecl>(D);
  458. }
  459. // Otherwise, we only care about anonymous class members / block-scope decls.
  460. // FIXME: We need to handle lambdas and blocks within inline / templated
  461. // variables too.
  462. if (D->getDeclName())
  463. return false;
  464. if (!isa<RecordDecl, ObjCInterfaceDecl>(D->getLexicalDeclContext()))
  465. return false;
  466. return isa<TagDecl, FieldDecl>(D);
  467. }