ModuleFileExtension.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- ModuleFileExtension.h - Module File Extensions ----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_SERIALIZATION_MODULEFILEEXTENSION_H
  14. #define LLVM_CLANG_SERIALIZATION_MODULEFILEEXTENSION_H
  15. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  16. #include "llvm/Support/ExtensibleRTTI.h"
  17. #include "llvm/Support/HashBuilder.h"
  18. #include "llvm/Support/MD5.h"
  19. #include <memory>
  20. #include <string>
  21. namespace llvm {
  22. class BitstreamCursor;
  23. class BitstreamWriter;
  24. class raw_ostream;
  25. }
  26. namespace clang {
  27. class ASTReader;
  28. class ASTWriter;
  29. class Sema;
  30. namespace serialization {
  31. class ModuleFile;
  32. } // end namespace serialization
  33. /// Metadata for a module file extension.
  34. struct ModuleFileExtensionMetadata {
  35. /// The name used to identify this particular extension block within
  36. /// the resulting module file. It should be unique to the particular
  37. /// extension, because this name will be used to match the name of
  38. /// an extension block to the appropriate reader.
  39. std::string BlockName;
  40. /// The major version of the extension data.
  41. unsigned MajorVersion;
  42. /// The minor version of the extension data.
  43. unsigned MinorVersion;
  44. /// A string containing additional user information that will be
  45. /// stored with the metadata.
  46. std::string UserInfo;
  47. };
  48. class ModuleFileExtensionReader;
  49. class ModuleFileExtensionWriter;
  50. /// An abstract superclass that describes a custom extension to the
  51. /// module/precompiled header file format.
  52. ///
  53. /// A module file extension can introduce additional information into
  54. /// compiled module files (.pcm) and precompiled headers (.pch) via a
  55. /// custom writer that can then be accessed via a custom reader when
  56. /// the module file or precompiled header is loaded.
  57. ///
  58. /// Subclasses must use LLVM RTTI for open class hierarchies.
  59. class ModuleFileExtension
  60. : public llvm::RTTIExtends<ModuleFileExtension, llvm::RTTIRoot> {
  61. public:
  62. /// Discriminator for LLVM RTTI.
  63. static char ID;
  64. virtual ~ModuleFileExtension();
  65. /// Retrieves the metadata for this module file extension.
  66. virtual ModuleFileExtensionMetadata getExtensionMetadata() const = 0;
  67. /// Hash information about the presence of this extension into the
  68. /// module hash.
  69. ///
  70. /// The module hash is used to distinguish different variants of a module that
  71. /// are incompatible. If the presence, absence, or version of the module file
  72. /// extension should force the creation of a separate set of module files,
  73. /// override this method to combine that distinguishing information into the
  74. /// module hash.
  75. ///
  76. /// The default implementation of this function simply does nothing, so the
  77. /// presence/absence of this extension does not distinguish module files.
  78. using ExtensionHashBuilder =
  79. llvm::HashBuilderImpl<llvm::MD5,
  80. llvm::support::endian::system_endianness()>;
  81. virtual void hashExtension(ExtensionHashBuilder &HBuilder) const;
  82. /// Create a new module file extension writer, which will be
  83. /// responsible for writing the extension contents into a particular
  84. /// module file.
  85. virtual std::unique_ptr<ModuleFileExtensionWriter>
  86. createExtensionWriter(ASTWriter &Writer) = 0;
  87. /// Create a new module file extension reader, given the
  88. /// metadata read from the block and the cursor into the extension
  89. /// block.
  90. ///
  91. /// May return null to indicate that an extension block with the
  92. /// given metadata cannot be read.
  93. virtual std::unique_ptr<ModuleFileExtensionReader>
  94. createExtensionReader(const ModuleFileExtensionMetadata &Metadata,
  95. ASTReader &Reader, serialization::ModuleFile &Mod,
  96. const llvm::BitstreamCursor &Stream) = 0;
  97. };
  98. /// Abstract base class that writes a module file extension block into
  99. /// a module file.
  100. class ModuleFileExtensionWriter {
  101. ModuleFileExtension *Extension;
  102. protected:
  103. ModuleFileExtensionWriter(ModuleFileExtension *Extension)
  104. : Extension(Extension) { }
  105. public:
  106. virtual ~ModuleFileExtensionWriter();
  107. /// Retrieve the module file extension with which this writer is
  108. /// associated.
  109. ModuleFileExtension *getExtension() const { return Extension; }
  110. /// Write the contents of the extension block into the given bitstream.
  111. ///
  112. /// Responsible for writing the contents of the extension into the
  113. /// given stream. All of the contents should be written into custom
  114. /// records with IDs >= FIRST_EXTENSION_RECORD_ID.
  115. virtual void writeExtensionContents(Sema &SemaRef,
  116. llvm::BitstreamWriter &Stream) = 0;
  117. };
  118. /// Abstract base class that reads a module file extension block from
  119. /// a module file.
  120. ///
  121. /// Subclasses
  122. class ModuleFileExtensionReader {
  123. ModuleFileExtension *Extension;
  124. protected:
  125. ModuleFileExtensionReader(ModuleFileExtension *Extension)
  126. : Extension(Extension) { }
  127. public:
  128. /// Retrieve the module file extension with which this reader is
  129. /// associated.
  130. ModuleFileExtension *getExtension() const { return Extension; }
  131. virtual ~ModuleFileExtensionReader();
  132. };
  133. } // end namespace clang
  134. #endif // LLVM_CLANG_SERIALIZATION_MODULEFILEEXTENSION_H
  135. #ifdef __GNUC__
  136. #pragma GCC diagnostic pop
  137. #endif