1336809Sdim//===- SmallVectorMemoryBuffer.h --------------------------------*- C++ -*-===//
2336809Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6336809Sdim//
7336809Sdim//===----------------------------------------------------------------------===//
8336809Sdim//
9336809Sdim// This file declares a wrapper class to hold the memory into which an
10336809Sdim// object will be generated.
11336809Sdim//
12336809Sdim//===----------------------------------------------------------------------===//
13336809Sdim
14336809Sdim#ifndef LLVM_EXECUTIONENGINE_OBJECTMEMORYBUFFER_H
15336809Sdim#define LLVM_EXECUTIONENGINE_OBJECTMEMORYBUFFER_H
16336809Sdim
17336809Sdim#include "llvm/ADT/SmallVector.h"
18336809Sdim#include "llvm/Support/MemoryBuffer.h"
19336809Sdim#include "llvm/Support/raw_ostream.h"
20336809Sdim
21336809Sdimnamespace llvm {
22336809Sdim
23336809Sdim/// SmallVector-backed MemoryBuffer instance.
24336809Sdim///
25336809Sdim/// This class enables efficient construction of MemoryBuffers from SmallVector
26336809Sdim/// instances. This is useful for MCJIT and Orc, where object files are streamed
27336809Sdim/// into SmallVectors, then inspected using ObjectFile (which takes a
28336809Sdim/// MemoryBuffer).
29336809Sdimclass SmallVectorMemoryBuffer : public MemoryBuffer {
30336809Sdimpublic:
31336809Sdim  /// Construct an SmallVectorMemoryBuffer from the given SmallVector
32336809Sdim  /// r-value.
33336809Sdim  ///
34336809Sdim  /// FIXME: It'd be nice for this to be a non-templated constructor taking a
35336809Sdim  /// SmallVectorImpl here instead of a templated one taking a SmallVector<N>,
36336809Sdim  /// but SmallVector's move-construction/assignment currently only take
37336809Sdim  /// SmallVectors. If/when that is fixed we can simplify this constructor and
38336809Sdim  /// the following one.
39336809Sdim  SmallVectorMemoryBuffer(SmallVectorImpl<char> &&SV)
40336809Sdim      : SV(std::move(SV)), BufferName("<in-memory object>") {
41336809Sdim    init(this->SV.begin(), this->SV.end(), false);
42336809Sdim  }
43336809Sdim
44336809Sdim  /// Construct a named SmallVectorMemoryBuffer from the given
45336809Sdim  /// SmallVector r-value and StringRef.
46336809Sdim  SmallVectorMemoryBuffer(SmallVectorImpl<char> &&SV, StringRef Name)
47336809Sdim      : SV(std::move(SV)), BufferName(Name) {
48336809Sdim    init(this->SV.begin(), this->SV.end(), false);
49336809Sdim  }
50336809Sdim
51337149Sdim  // Key function.
52337149Sdim  ~SmallVectorMemoryBuffer() override;
53337149Sdim
54336809Sdim  StringRef getBufferIdentifier() const override { return BufferName; }
55336809Sdim
56336809Sdim  BufferKind getBufferKind() const override { return MemoryBuffer_Malloc; }
57336809Sdim
58336809Sdimprivate:
59336809Sdim  SmallVector<char, 0> SV;
60336809Sdim  std::string BufferName;
61336809Sdim};
62336809Sdim
63336809Sdim} // namespace llvm
64336809Sdim
65336809Sdim#endif
66