]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Frontend/PCHContainerOperations.h
Merge ^/head r341764 through r341812.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Frontend / PCHContainerOperations.h
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 "clang/Basic/Module.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include <memory>
18
19 namespace llvm {
20 class raw_pwrite_stream;
21 }
22
23 using llvm::StringRef;
24
25 namespace clang {
26
27 class ASTConsumer;
28 class CodeGenOptions;
29 class DiagnosticsEngine;
30 class CompilerInstance;
31
32 struct PCHBuffer {
33   ASTFileSignature Signature;
34   llvm::SmallVector<char, 0> Data;
35   bool IsComplete;
36 };
37
38 /// This abstract interface provides operations for creating
39 /// containers for serialized ASTs (precompiled headers and clang
40 /// modules).
41 class PCHContainerWriter {
42 public:
43   virtual ~PCHContainerWriter() = 0;
44   virtual StringRef getFormat() const = 0;
45
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;
55 };
56
57 /// This abstract interface provides operations for unwrapping
58 /// containers for serialized ASTs (precompiled headers and clang
59 /// modules).
60 class PCHContainerReader {
61 public:
62   virtual ~PCHContainerReader() = 0;
63   /// Equivalent to the format passed to -fmodule-format=
64   virtual StringRef getFormat() const = 0;
65
66   /// Returns the serialized AST inside the PCH container Buffer.
67   virtual StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const = 0;
68 };
69
70 /// Implements write operations for a raw pass-through PCH container.
71 class RawPCHContainerWriter : public PCHContainerWriter {
72   StringRef getFormat() const override { return "raw"; }
73
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;
82 };
83
84 /// Implements read operations for a raw pass-through PCH container.
85 class RawPCHContainerReader : public PCHContainerReader {
86   StringRef getFormat() const override { return "raw"; }
87
88   /// Simply returns the buffer contained in Buffer.
89   StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const override;
90 };
91
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;
96 public:
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);
102   }
103   void registerReader(std::unique_ptr<PCHContainerReader> Reader) {
104     Readers[Reader->getFormat()] = std::move(Reader);
105   }
106   const PCHContainerWriter *getWriterOrNull(StringRef Format) {
107     return Writers[Format].get();
108   }
109   const PCHContainerReader *getReaderOrNull(StringRef Format) {
110     return Readers[Format].get();
111   }
112   const PCHContainerReader &getRawReader() {
113     return *getReaderOrNull("raw");
114   }
115 };
116
117 }
118
119 #endif