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 "clang/Basic/Module.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/Support/MemoryBuffer.h"
20 class raw_pwrite_stream;
23 using llvm::StringRef;
29 class DiagnosticsEngine;
30 class CompilerInstance;
33 ASTFileSignature Signature;
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>
50 CreatePCHContainerGenerator(CompilerInstance &CI,
51 const std::string &MainFileName,
52 const std::string &OutputFileName,
53 std::unique_ptr<llvm::raw_pwrite_stream> OS,
54 std::shared_ptr<PCHBuffer> Buffer) const = 0;
57 /// This abstract interface provides operations for unwrapping
58 /// containers for serialized ASTs (precompiled headers and clang
60 class PCHContainerReader {
62 virtual ~PCHContainerReader() = 0;
63 /// Equivalent to the format passed to -fmodule-format=
64 virtual StringRef getFormat() const = 0;
66 /// Returns the serialized AST inside the PCH container Buffer.
67 virtual StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) 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>
77 CreatePCHContainerGenerator(CompilerInstance &CI,
78 const std::string &MainFileName,
79 const std::string &OutputFileName,
80 std::unique_ptr<llvm::raw_pwrite_stream> OS,
81 std::shared_ptr<PCHBuffer> Buffer) const override;
84 /// Implements read operations for a raw pass-through PCH container.
85 class RawPCHContainerReader : public PCHContainerReader {
86 StringRef getFormat() const override { return "raw"; }
88 /// Simply returns the buffer contained in Buffer.
89 StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const override;
92 /// A registry of PCHContainerWriter and -Reader objects for different formats.
93 class PCHContainerOperations {
94 llvm::StringMap<std::unique_ptr<PCHContainerWriter>> Writers;
95 llvm::StringMap<std::unique_ptr<PCHContainerReader>> Readers;
97 /// Automatically registers a RawPCHContainerWriter and
98 /// RawPCHContainerReader.
99 PCHContainerOperations();
100 void registerWriter(std::unique_ptr<PCHContainerWriter> Writer) {
101 Writers[Writer->getFormat()] = std::move(Writer);
103 void registerReader(std::unique_ptr<PCHContainerReader> Reader) {
104 Readers[Reader->getFormat()] = std::move(Reader);
106 const PCHContainerWriter *getWriterOrNull(StringRef Format) {
107 return Writers[Format].get();
109 const PCHContainerReader *getReaderOrNull(StringRef Format) {
110 return Readers[Format].get();
112 const PCHContainerReader &getRawReader() {
113 return *getReaderOrNull("raw");