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 CompilerInstance;
34 llvm::SmallVector<char, 0> Data;
38 /// This abstract interface provides operations for creating
39 /// containers for serialized ASTs (precompiled headers and clang
41 class PCHContainerWriter {
43 virtual ~PCHContainerWriter() = 0;
44 virtual StringRef getFormat() const = 0;
46 /// Return an ASTConsumer that can be chained with a
47 /// PCHGenerator that produces a wrapper file format containing a
48 /// serialized AST bitstream.
49 virtual std::unique_ptr<ASTConsumer> CreatePCHContainerGenerator(
50 CompilerInstance &CI, const std::string &MainFileName,
51 const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
52 std::shared_ptr<PCHBuffer> Buffer) const = 0;
55 /// This abstract interface provides operations for unwrapping
56 /// containers for serialized ASTs (precompiled headers and clang
58 class PCHContainerReader {
60 virtual ~PCHContainerReader() = 0;
61 /// Equivalent to the format passed to -fmodule-format=
62 virtual StringRef getFormat() const = 0;
64 /// Initialize an llvm::BitstreamReader with the serialized AST inside
65 /// the PCH container Buffer.
66 virtual void ExtractPCH(llvm::MemoryBufferRef Buffer,
67 llvm::BitstreamReader &StreamFile) const = 0;
70 /// Implements write operations for a raw pass-through PCH container.
71 class RawPCHContainerWriter : public PCHContainerWriter {
72 StringRef getFormat() const override { return "raw"; }
74 /// Return an ASTConsumer that can be chained with a
75 /// PCHGenerator that writes the module to a flat file.
76 std::unique_ptr<ASTConsumer> CreatePCHContainerGenerator(
77 CompilerInstance &CI, const std::string &MainFileName,
78 const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
79 std::shared_ptr<PCHBuffer> Buffer) const override;
82 /// Implements read operations for a raw pass-through PCH container.
83 class RawPCHContainerReader : public PCHContainerReader {
84 StringRef getFormat() const override { return "raw"; }
86 /// Initialize an llvm::BitstreamReader with Buffer.
87 void ExtractPCH(llvm::MemoryBufferRef Buffer,
88 llvm::BitstreamReader &StreamFile) const override;
91 /// A registry of PCHContainerWriter and -Reader objects for different formats.
92 class PCHContainerOperations {
93 llvm::StringMap<std::unique_ptr<PCHContainerWriter>> Writers;
94 llvm::StringMap<std::unique_ptr<PCHContainerReader>> Readers;
96 /// Automatically registers a RawPCHContainerWriter and
97 /// RawPCHContainerReader.
98 PCHContainerOperations();
99 void registerWriter(std::unique_ptr<PCHContainerWriter> Writer) {
100 Writers[Writer->getFormat()] = std::move(Writer);
102 void registerReader(std::unique_ptr<PCHContainerReader> Reader) {
103 Readers[Reader->getFormat()] = std::move(Reader);
105 const PCHContainerWriter *getWriterOrNull(StringRef Format) {
106 return Writers[Format].get();
108 const PCHContainerReader *getReaderOrNull(StringRef Format) {
109 return Readers[Format].get();
111 const PCHContainerReader &getRawReader() {
112 return *getReaderOrNull("raw");