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
19namespace llvm {
20class raw_pwrite_stream;
21}
22
23using llvm::StringRef;
24
25namespace clang {
26
27class ASTConsumer;
28class CodeGenOptions;
29class DiagnosticsEngine;
30class CompilerInstance;
31
32struct 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).
41class PCHContainerWriter {
42public:
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).
60class PCHContainerReader {
61public:
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.
71class 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.
85class 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.
93class PCHContainerOperations {
94  llvm::StringMap<std::unique_ptr<PCHContainerWriter>> Writers;
95  llvm::StringMap<std::unique_ptr<PCHContainerReader>> Readers;
96public:
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
120