ASTCommon.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //===- ASTCommon.h - 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. #ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
  13. #define LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/DeclFriend.h"
  16. #include "clang/Serialization/ASTBitCodes.h"
  17. namespace clang {
  18. namespace serialization {
  19. enum DeclUpdateKind {
  20. UPD_CXX_ADDED_IMPLICIT_MEMBER,
  21. UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
  22. UPD_CXX_ADDED_ANONYMOUS_NAMESPACE,
  23. UPD_CXX_ADDED_FUNCTION_DEFINITION,
  24. UPD_CXX_ADDED_VAR_DEFINITION,
  25. UPD_CXX_POINT_OF_INSTANTIATION,
  26. UPD_CXX_INSTANTIATED_CLASS_DEFINITION,
  27. UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT,
  28. UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER,
  29. UPD_CXX_RESOLVED_DTOR_DELETE,
  30. UPD_CXX_RESOLVED_EXCEPTION_SPEC,
  31. UPD_CXX_DEDUCED_RETURN_TYPE,
  32. UPD_DECL_MARKED_USED,
  33. UPD_MANGLING_NUMBER,
  34. UPD_STATIC_LOCAL_NUMBER,
  35. UPD_DECL_MARKED_OPENMP_THREADPRIVATE,
  36. UPD_DECL_MARKED_OPENMP_ALLOCATE,
  37. UPD_DECL_MARKED_OPENMP_DECLARETARGET,
  38. UPD_DECL_EXPORTED,
  39. UPD_ADDED_ATTR_TO_RECORD
  40. };
  41. TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT);
  42. template <typename IdxForTypeTy>
  43. TypeID MakeTypeID(ASTContext &Context, QualType T, IdxForTypeTy IdxForType) {
  44. if (T.isNull())
  45. return PREDEF_TYPE_NULL_ID;
  46. unsigned FastQuals = T.getLocalFastQualifiers();
  47. T.removeLocalFastQualifiers();
  48. if (T.hasLocalNonFastQualifiers())
  49. return IdxForType(T).asTypeID(FastQuals);
  50. assert(!T.hasLocalQualifiers());
  51. if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr()))
  52. return TypeIdxFromBuiltin(BT).asTypeID(FastQuals);
  53. if (T == Context.AutoDeductTy)
  54. return TypeIdx(PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
  55. if (T == Context.AutoRRefDeductTy)
  56. return TypeIdx(PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
  57. return IdxForType(T).asTypeID(FastQuals);
  58. }
  59. unsigned ComputeHash(Selector Sel);
  60. /// Retrieve the "definitive" declaration that provides all of the
  61. /// visible entries for the given declaration context, if there is one.
  62. ///
  63. /// The "definitive" declaration is the only place where we need to look to
  64. /// find information about the declarations within the given declaration
  65. /// context. For example, C++ and Objective-C classes, C structs/unions, and
  66. /// Objective-C protocols, categories, and extensions are all defined in a
  67. /// single place in the source code, so they have definitive declarations
  68. /// associated with them. C++ namespaces, on the other hand, can have
  69. /// multiple definitions.
  70. const DeclContext *getDefinitiveDeclContext(const DeclContext *DC);
  71. /// Determine whether the given declaration kind is redeclarable.
  72. bool isRedeclarableDeclKind(unsigned Kind);
  73. /// Determine whether the given declaration needs an anonymous
  74. /// declaration number.
  75. bool needsAnonymousDeclarationNumber(const NamedDecl *D);
  76. /// Visit each declaration within \c DC that needs an anonymous
  77. /// declaration number and call \p Visit with the declaration and its number.
  78. template<typename Fn> void numberAnonymousDeclsWithin(const DeclContext *DC,
  79. Fn Visit) {
  80. unsigned Index = 0;
  81. for (Decl *LexicalD : DC->decls()) {
  82. // For a friend decl, we care about the declaration within it, if any.
  83. if (auto *FD = dyn_cast<FriendDecl>(LexicalD))
  84. LexicalD = FD->getFriendDecl();
  85. auto *ND = dyn_cast_or_null<NamedDecl>(LexicalD);
  86. if (!ND || !needsAnonymousDeclarationNumber(ND))
  87. continue;
  88. Visit(ND, Index++);
  89. }
  90. }
  91. /// Determine whether the given declaration will be included in the per-module
  92. /// initializer if it needs to be eagerly handed to the AST consumer. If so, we
  93. /// should not hand it to the consumer when deserializing it, nor include it in
  94. /// the list of eagerly deserialized declarations.
  95. inline bool isPartOfPerModuleInitializer(const Decl *D) {
  96. if (isa<ImportDecl>(D))
  97. return true;
  98. // Template instantiations are notionally in an "instantiation unit" rather
  99. // than in any particular translation unit, so they need not be part of any
  100. // particular (sub)module's per-module initializer.
  101. if (auto *VD = dyn_cast<VarDecl>(D))
  102. return !isTemplateInstantiation(VD->getTemplateSpecializationKind());
  103. return false;
  104. }
  105. } // namespace serialization
  106. } // namespace clang
  107. #endif