ASTCommon.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //===- ASTCommon.h - 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. #ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
  14. #define LLVM_CLANG_LIB_SERIALIZATION_ASTCOMMON_H
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/DeclFriend.h"
  17. #include "clang/Serialization/ASTBitCodes.h"
  18. namespace clang {
  19. namespace serialization {
  20. enum DeclUpdateKind {
  21. UPD_CXX_ADDED_IMPLICIT_MEMBER,
  22. UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
  23. UPD_CXX_ADDED_ANONYMOUS_NAMESPACE,
  24. UPD_CXX_ADDED_FUNCTION_DEFINITION,
  25. UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
  26. UPD_CXX_INSTANTIATED_CLASS_DEFINITION,
  27. UPD_CXX_RESOLVED_DTOR_DELETE,
  28. UPD_CXX_RESOLVED_EXCEPTION_SPEC,
  29. UPD_CXX_DEDUCED_RETURN_TYPE,
  30. UPD_DECL_MARKED_USED,
  31. UPD_MANGLING_NUMBER,
  32. UPD_STATIC_LOCAL_NUMBER,
  33. UPD_DECL_MARKED_OPENMP_THREADPRIVATE,
  34. UPD_DECL_EXPORTED,
  35. UPD_ADDED_ATTR_TO_RECORD
  36. };
  37. TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT);
  38. template <typename IdxForTypeTy>
  39. TypeID MakeTypeID(ASTContext &Context, QualType T, IdxForTypeTy IdxForType) {
  40. if (T.isNull())
  41. return PREDEF_TYPE_NULL_ID;
  42. unsigned FastQuals = T.getLocalFastQualifiers();
  43. T.removeLocalFastQualifiers();
  44. if (T.hasLocalNonFastQualifiers())
  45. return IdxForType(T).asTypeID(FastQuals);
  46. assert(!T.hasLocalQualifiers());
  47. if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr()))
  48. return TypeIdxFromBuiltin(BT).asTypeID(FastQuals);
  49. if (T == Context.AutoDeductTy)
  50. return TypeIdx(PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
  51. if (T == Context.AutoRRefDeductTy)
  52. return TypeIdx(PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
  53. if (T == Context.VaListTagTy)
  54. return TypeIdx(PREDEF_TYPE_VA_LIST_TAG).asTypeID(FastQuals);
  55. return IdxForType(T).asTypeID(FastQuals);
  56. }
  57. unsigned ComputeHash(Selector Sel);
  58. /// \brief Retrieve the "definitive" declaration that provides all of the
  59. /// visible entries for the given declaration context, if there is one.
  60. ///
  61. /// The "definitive" declaration is the only place where we need to look to
  62. /// find information about the declarations within the given declaration
  63. /// context. For example, C++ and Objective-C classes, C structs/unions, and
  64. /// Objective-C protocols, categories, and extensions are all defined in a
  65. /// single place in the source code, so they have definitive declarations
  66. /// associated with them. C++ namespaces, on the other hand, can have
  67. /// multiple definitions.
  68. const DeclContext *getDefinitiveDeclContext(const DeclContext *DC);
  69. /// \brief Determine whether the given declaration kind is redeclarable.
  70. bool isRedeclarableDeclKind(unsigned Kind);
  71. /// \brief Determine whether the given declaration needs an anonymous
  72. /// declaration number.
  73. bool needsAnonymousDeclarationNumber(const NamedDecl *D);
  74. /// \brief Visit each declaration within \c DC that needs an anonymous
  75. /// declaration number and call \p Visit with the declaration and its number.
  76. template<typename Fn> void numberAnonymousDeclsWithin(const DeclContext *DC,
  77. Fn Visit) {
  78. unsigned Index = 0;
  79. for (Decl *LexicalD : DC->decls()) {
  80. // For a friend decl, we care about the declaration within it, if any.
  81. if (auto *FD = dyn_cast<FriendDecl>(LexicalD))
  82. LexicalD = FD->getFriendDecl();
  83. auto *ND = dyn_cast_or_null<NamedDecl>(LexicalD);
  84. if (!ND || !needsAnonymousDeclarationNumber(ND))
  85. continue;
  86. Visit(ND, Index++);
  87. }
  88. }
  89. } // namespace serialization
  90. } // namespace clang
  91. #endif