1 //===--- Frontend/PCHContainerOperations.h - PCH Containers -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_PCH_CONTAINER_OPERATIONS_H
11 #define LLVM_CLANG_PCH_CONTAINER_OPERATIONS_H
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/Support/MemoryBuffer.h"
19 class raw_pwrite_stream;
20 class BitstreamReader;
23 using llvm::StringRef;
29 class DiagnosticsEngine;
30 class HeaderSearchOptions;
32 class PreprocessorOptions;
37 llvm::SmallVector<char, 0> Data;
40 /// This abstract interface provides operations for creating
41 /// containers for serialized ASTs (precompiled headers and clang
43 class PCHContainerWriter {
45 virtual ~PCHContainerWriter() = 0;
46 virtual StringRef getFormat() const = 0;
48 /// Return an ASTConsumer that can be chained with a
49 /// PCHGenerator that produces a wrapper file format containing a
50 /// serialized AST bitstream.
51 virtual std::unique_ptr<ASTConsumer> CreatePCHContainerGenerator(
52 DiagnosticsEngine &Diags, const HeaderSearchOptions &HSO,
53 const PreprocessorOptions &PPO, const TargetOptions &TO,
54 const LangOptions &LO, const std::string &MainFileName,
55 const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
56 std::shared_ptr<PCHBuffer> Buffer) const = 0;
59 /// This abstract interface provides operations for unwrapping
60 /// containers for serialized ASTs (precompiled headers and clang
62 class PCHContainerReader {
64 virtual ~PCHContainerReader() = 0;
65 /// Equivalent to the format passed to -fmodule-format=
66 virtual StringRef getFormat() const = 0;
68 /// Initialize an llvm::BitstreamReader with the serialized AST inside
69 /// the PCH container Buffer.
70 virtual void ExtractPCH(llvm::MemoryBufferRef Buffer,
71 llvm::BitstreamReader &StreamFile) const = 0;
74 /// Implements write operations for a raw pass-through PCH container.
75 class RawPCHContainerWriter : public PCHContainerWriter {
76 StringRef getFormat() const override { return "raw"; }
78 /// Return an ASTConsumer that can be chained with a
79 /// PCHGenerator that writes the module to a flat file.
80 std::unique_ptr<ASTConsumer> CreatePCHContainerGenerator(
81 DiagnosticsEngine &Diags, const HeaderSearchOptions &HSO,
82 const PreprocessorOptions &PPO, const TargetOptions &TO,
83 const LangOptions &LO, const std::string &MainFileName,
84 const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
85 std::shared_ptr<PCHBuffer> Buffer) const override;
88 /// Implements read operations for a raw pass-through PCH container.
89 class RawPCHContainerReader : public PCHContainerReader {
90 StringRef getFormat() const override { return "raw"; }
92 /// Initialize an llvm::BitstreamReader with Buffer.
93 void ExtractPCH(llvm::MemoryBufferRef Buffer,
94 llvm::BitstreamReader &StreamFile) const override;
97 /// A registry of PCHContainerWriter and -Reader objects for different formats.
98 class PCHContainerOperations {
99 llvm::StringMap<std::unique_ptr<PCHContainerWriter>> Writers;
100 llvm::StringMap<std::unique_ptr<PCHContainerReader>> Readers;
102 /// Automatically registers a RawPCHContainerWriter and
103 /// RawPCHContainerReader.
104 PCHContainerOperations();
105 void registerWriter(std::unique_ptr<PCHContainerWriter> Writer) {
106 Writers[Writer->getFormat()] = std::move(Writer);
108 void registerReader(std::unique_ptr<PCHContainerReader> Reader) {
109 Readers[Reader->getFormat()] = std::move(Reader);
111 const PCHContainerWriter *getWriterOrNull(StringRef Format) {
112 return Writers[Format].get();
114 const PCHContainerReader *getReaderOrNull(StringRef Format) {
115 return Readers[Format].get();
117 const PCHContainerReader &getRawReader() {
118 return *getReaderOrNull("raw");