]> git.proxmox.com Git - rustc.git/blame - src/llvm/include/llvm/Support/MemoryBuffer.h
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / include / llvm / Support / MemoryBuffer.h
CommitLineData
223e47cc
LB
1//===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- 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// This file defines the MemoryBuffer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_MEMORYBUFFER_H
15#define LLVM_SUPPORT_MEMORYBUFFER_H
16
1a4d82fc
JJ
17#include "llvm-c/Support.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/Support/CBindingWrapping.h"
223e47cc
LB
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/DataTypes.h"
1a4d82fc
JJ
22#include "llvm/Support/ErrorOr.h"
23#include <memory>
24#include <system_error>
223e47cc
LB
25
26namespace llvm {
1a4d82fc 27class MemoryBufferRef;
223e47cc 28
1a4d82fc
JJ
29/// This interface provides simple read-only access to a block of memory, and
30/// provides simple methods for reading files and standard input into a memory
31/// buffer. In addition to basic access to the characters in the file, this
32/// interface guarantees you can read one character past the end of the file,
33/// and that this character will read as '\0'.
223e47cc
LB
34///
35/// The '\0' guarantee is needed to support an optimization -- it's intended to
36/// be more efficient for clients which are reading all the data to stop
37/// reading when they encounter a '\0' than to continually check the file
38/// position to see if it has reached the end of the file.
39class MemoryBuffer {
40 const char *BufferStart; // Start of the buffer.
41 const char *BufferEnd; // End of the buffer.
42
43 MemoryBuffer(const MemoryBuffer &) LLVM_DELETED_FUNCTION;
44 MemoryBuffer &operator=(const MemoryBuffer &) LLVM_DELETED_FUNCTION;
45protected:
46 MemoryBuffer() {}
47 void init(const char *BufStart, const char *BufEnd,
48 bool RequiresNullTerminator);
49public:
50 virtual ~MemoryBuffer();
51
52 const char *getBufferStart() const { return BufferStart; }
53 const char *getBufferEnd() const { return BufferEnd; }
54 size_t getBufferSize() const { return BufferEnd-BufferStart; }
55
56 StringRef getBuffer() const {
57 return StringRef(BufferStart, getBufferSize());
58 }
59
1a4d82fc
JJ
60 /// Return an identifier for this buffer, typically the filename it was read
61 /// from.
223e47cc
LB
62 virtual const char *getBufferIdentifier() const {
63 return "Unknown buffer";
64 }
65
1a4d82fc
JJ
66 /// Open the specified file as a MemoryBuffer, returning a new MemoryBuffer
67 /// if successful, otherwise returning null. If FileSize is specified, this
68 /// means that the client knows that the file exists and that it has the
69 /// specified size.
70 ///
71 /// \param IsVolatileSize Set to true to indicate that the file size may be
72 /// changing, e.g. when libclang tries to parse while the user is
73 /// editing/updating the file.
74 static ErrorOr<std::unique_ptr<MemoryBuffer>>
85aaf69f 75 getFile(const Twine &Filename, int64_t FileSize = -1,
1a4d82fc
JJ
76 bool RequiresNullTerminator = true, bool IsVolatileSize = false);
77
78 /// Given an already-open file descriptor, map some slice of it into a
79 /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
80 /// Since this is in the middle of a file, the buffer is not null terminated.
1a4d82fc 81 static ErrorOr<std::unique_ptr<MemoryBuffer>>
85aaf69f
SL
82 getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
83 int64_t Offset);
1a4d82fc
JJ
84
85 /// Given an already-open file descriptor, read the file and return a
86 /// MemoryBuffer.
87 ///
88 /// \param IsVolatileSize Set to true to indicate that the file size may be
89 /// changing, e.g. when libclang tries to parse while the user is
90 /// editing/updating the file.
91 static ErrorOr<std::unique_ptr<MemoryBuffer>>
85aaf69f 92 getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
1a4d82fc
JJ
93 bool RequiresNullTerminator = true, bool IsVolatileSize = false);
94
95 /// Open the specified memory range as a MemoryBuffer. Note that InputData
96 /// must be null terminated if RequiresNullTerminator is true.
97 static std::unique_ptr<MemoryBuffer>
98 getMemBuffer(StringRef InputData, StringRef BufferName = "",
99 bool RequiresNullTerminator = true);
100
101 static std::unique_ptr<MemoryBuffer>
102 getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
103
104 /// Open the specified memory range as a MemoryBuffer, copying the contents
105 /// and taking ownership of it. InputData does not have to be null terminated.
106 static std::unique_ptr<MemoryBuffer>
85aaf69f 107 getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
1a4d82fc
JJ
108
109 /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
110 /// that the caller need not initialize the memory allocated by this method.
111 /// The memory is owned by the MemoryBuffer object.
112 static std::unique_ptr<MemoryBuffer>
113 getNewMemBuffer(size_t Size, StringRef BufferName = "");
114
115 /// Allocate a new MemoryBuffer of the specified size that is not initialized.
116 /// Note that the caller should initialize the memory allocated by this
117 /// method. The memory is owned by the MemoryBuffer object.
118 static std::unique_ptr<MemoryBuffer>
85aaf69f 119 getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "");
1a4d82fc
JJ
120
121 /// Read all of stdin into a file buffer, and return it.
122 static ErrorOr<std::unique_ptr<MemoryBuffer>> getSTDIN();
123
124 /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
125 /// is "-".
126 static ErrorOr<std::unique_ptr<MemoryBuffer>>
85aaf69f
SL
127 getFileOrSTDIN(const Twine &Filename, int64_t FileSize = -1);
128
129 /// Map a subrange of the the specified file as a MemoryBuffer.
130 static ErrorOr<std::unique_ptr<MemoryBuffer>>
131 getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset);
1a4d82fc 132
223e47cc
LB
133 //===--------------------------------------------------------------------===//
134 // Provided for performance analysis.
135 //===--------------------------------------------------------------------===//
136
137 /// The kind of memory backing used to support the MemoryBuffer.
138 enum BufferKind {
139 MemoryBuffer_Malloc,
140 MemoryBuffer_MMap
141 };
142
143 /// Return information on the memory mechanism used to support the
144 /// MemoryBuffer.
1a4d82fc
JJ
145 virtual BufferKind getBufferKind() const = 0;
146
147 MemoryBufferRef getMemBufferRef() const;
148};
149
150class MemoryBufferRef {
151 StringRef Buffer;
152 StringRef Identifier;
153
154public:
155 MemoryBufferRef() {}
156 MemoryBufferRef(StringRef Buffer, StringRef Identifier)
157 : Buffer(Buffer), Identifier(Identifier) {}
158
159 StringRef getBuffer() const { return Buffer; }
160
161 StringRef getBufferIdentifier() const { return Identifier; }
162
163 const char *getBufferStart() const { return Buffer.begin(); }
164 const char *getBufferEnd() const { return Buffer.end(); }
165 size_t getBufferSize() const { return Buffer.size(); }
223e47cc
LB
166};
167
1a4d82fc
JJ
168// Create wrappers for C Binding types (see CBindingWrapping.h).
169DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef)
170
223e47cc
LB
171} // end namespace llvm
172
173#endif