Line data Source code
1 : //===--- Frontend/PCHContainerOperations.h - PCH Containers -----*- 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 : #ifndef LLVM_CLANG_PCH_CONTAINER_OPERATIONS_H
11 : #define LLVM_CLANG_PCH_CONTAINER_OPERATIONS_H
12 :
13 : #include "llvm/ADT/SmallVector.h"
14 : #include "llvm/ADT/StringMap.h"
15 : #include "llvm/Support/MemoryBuffer.h"
16 : #include <memory>
17 :
18 : namespace llvm {
19 : class raw_pwrite_stream;
20 : class BitstreamReader;
21 : }
22 :
23 : using llvm::StringRef;
24 :
25 : namespace clang {
26 :
27 : class ASTConsumer;
28 : class CodeGenOptions;
29 : class DiagnosticsEngine;
30 : class HeaderSearchOptions;
31 : class LangOptions;
32 : class PreprocessorOptions;
33 : class TargetOptions;
34 :
35 : struct PCHBuffer {
36 : bool IsComplete;
37 : llvm::SmallVector<char, 0> Data;
38 : };
39 :
40 : /// This abstract interface provides operations for creating
41 : /// containers for serialized ASTs (precompiled headers and clang
42 : /// modules).
43 : class PCHContainerWriter {
44 : public:
45 : virtual ~PCHContainerWriter() = 0;
46 : virtual StringRef getFormat() const = 0;
47 :
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;
57 : };
58 :
59 : /// This abstract interface provides operations for unwrapping
60 : /// containers for serialized ASTs (precompiled headers and clang
61 : /// modules).
62 : class PCHContainerReader {
63 : public:
64 : virtual ~PCHContainerReader() = 0;
65 : /// Equivalent to the format passed to -fmodule-format=
66 : virtual StringRef getFormat() const = 0;
67 :
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;
72 : };
73 :
74 : /// Implements write operations for a raw pass-through PCH container.
75 : class RawPCHContainerWriter : public PCHContainerWriter {
76 : StringRef getFormat() const override { return "raw"; }
77 :
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;
86 : };
87 :
88 : /// Implements read operations for a raw pass-through PCH container.
89 : class RawPCHContainerReader : public PCHContainerReader {
90 : StringRef getFormat() const override { return "raw"; }
91 :
92 : /// Initialize an llvm::BitstreamReader with Buffer.
93 : void ExtractPCH(llvm::MemoryBufferRef Buffer,
94 : llvm::BitstreamReader &StreamFile) const override;
95 : };
96 :
97 : /// A registry of PCHContainerWriter and -Reader objects for different formats.
98 12 : class PCHContainerOperations {
99 : llvm::StringMap<std::unique_ptr<PCHContainerWriter>> Writers;
100 : llvm::StringMap<std::unique_ptr<PCHContainerReader>> Readers;
101 : public:
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);
107 : }
108 : void registerReader(std::unique_ptr<PCHContainerReader> Reader) {
109 : Readers[Reader->getFormat()] = std::move(Reader);
110 : }
111 : const PCHContainerWriter *getWriterOrNull(StringRef Format) {
112 : return Writers[Format].get();
113 : }
114 : const PCHContainerReader *getReaderOrNull(StringRef Format) {
115 : return Readers[Format].get();
116 : }
117 : const PCHContainerReader &getRawReader() {
118 : return *getReaderOrNull("raw");
119 : }
120 : };
121 :
122 : }
123 :
124 : #endif
|