[Support/MemoryBuffer] Rename IsVolatile -> IsVolatileSize and add a comment about...
[oota-llvm.git] / include / llvm / Support / MemoryBuffer.h
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
17 #include "llvm-c/Support.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/CBindingWrapping.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/DataTypes.h"
22 #include <memory>
23
24 namespace llvm {
25
26 class error_code;
27 template<class T> class OwningPtr;
28
29 /// MemoryBuffer - This interface provides simple read-only access to a block
30 /// of memory, and provides simple methods for reading files and standard input
31 /// into a memory buffer.  In addition to basic access to the characters in the
32 /// file, this interface guarantees you can read one character past the end of
33 /// the file, and that this character will read as '\0'.
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.
39 class 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;
45 protected:
46   MemoryBuffer() {}
47   void init(const char *BufStart, const char *BufEnd,
48             bool RequiresNullTerminator);
49 public:
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
60   /// getBufferIdentifier - Return an identifier for this buffer, typically the
61   /// filename it was read from.
62   virtual const char *getBufferIdentifier() const {
63     return "Unknown buffer";
64   }
65
66   /// getFile - Open the specified file as a MemoryBuffer, returning a new
67   /// MemoryBuffer if successful, otherwise returning null.  If FileSize is
68   /// specified, this means that the client knows that the file exists and that
69   /// it has the 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 error_code getFile(Twine Filename, OwningPtr<MemoryBuffer> &Result,
75                             int64_t FileSize = -1,
76                             bool RequiresNullTerminator = true,
77                             bool IsVolatileSize = false);
78   static error_code getFile(Twine Filename,
79                             std::unique_ptr<MemoryBuffer> &Result,
80                             int64_t FileSize = -1,
81                             bool RequiresNullTerminator = true,
82                             bool IsVolatileSize = false);
83
84   /// Given an already-open file descriptor, map some slice of it into a
85   /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
86   /// Since this is in the middle of a file, the buffer is not null terminated.
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 error_code getOpenFileSlice(int FD, const char *Filename,
92                                      OwningPtr<MemoryBuffer> &Result,
93                                      uint64_t MapSize, int64_t Offset,
94                                      bool IsVolatileSize = false);
95   static error_code getOpenFileSlice(int FD, const char *Filename,
96                                      std::unique_ptr<MemoryBuffer> &Result,
97                                      uint64_t MapSize, int64_t Offset,
98                                      bool IsVolatileSize = false);
99
100   /// Given an already-open file descriptor, read the file and return a
101   /// MemoryBuffer.
102   ///
103   /// \param IsVolatileSize Set to true to indicate that the file size may be
104   /// changing, e.g. when libclang tries to parse while the user is
105   /// editing/updating the file.
106   static error_code getOpenFile(int FD, const char *Filename,
107                                 OwningPtr<MemoryBuffer> &Result,
108                                 uint64_t FileSize,
109                                 bool RequiresNullTerminator = true,
110                                 bool IsVolatileSize = false);
111   static error_code getOpenFile(int FD, const char *Filename,
112                                 std::unique_ptr<MemoryBuffer> &Result,
113                                 uint64_t FileSize,
114                                 bool RequiresNullTerminator = true,
115                                 bool IsVolatileSize = false);
116
117   /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
118   /// that InputData must be null terminated if RequiresNullTerminator is true.
119   static MemoryBuffer *getMemBuffer(StringRef InputData,
120                                     StringRef BufferName = "",
121                                     bool RequiresNullTerminator = true);
122
123   /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
124   /// copying the contents and taking ownership of it.  InputData does not
125   /// have to be null terminated.
126   static MemoryBuffer *getMemBufferCopy(StringRef InputData,
127                                         StringRef BufferName = "");
128
129   /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
130   /// is completely initialized to zeros.  Note that the caller should
131   /// initialize the memory allocated by this method.  The memory is owned by
132   /// the MemoryBuffer object.
133   static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
134
135   /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
136   /// that is not initialized.  Note that the caller should initialize the
137   /// memory allocated by this method.  The memory is owned by the MemoryBuffer
138   /// object.
139   static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
140                                              StringRef BufferName = "");
141
142   /// getSTDIN - Read all of stdin into a file buffer, and return it.
143   /// If an error occurs, this returns null and sets ec.
144   static error_code getSTDIN(OwningPtr<MemoryBuffer> &Result);
145   static error_code getSTDIN(std::unique_ptr<MemoryBuffer> &Result);
146
147
148   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
149   /// if the Filename is "-".  If an error occurs, this returns null and sets
150   /// ec.
151   static error_code getFileOrSTDIN(StringRef Filename,
152                                    OwningPtr<MemoryBuffer> &Result,
153                                    int64_t FileSize = -1);
154   static error_code getFileOrSTDIN(StringRef Filename,
155                                    std::unique_ptr<MemoryBuffer> &Result,
156                                    int64_t FileSize = -1);
157
158   //===--------------------------------------------------------------------===//
159   // Provided for performance analysis.
160   //===--------------------------------------------------------------------===//
161
162   /// The kind of memory backing used to support the MemoryBuffer.
163   enum BufferKind {
164     MemoryBuffer_Malloc,
165     MemoryBuffer_MMap
166   };
167
168   /// Return information on the memory mechanism used to support the
169   /// MemoryBuffer.
170   virtual BufferKind getBufferKind() const = 0;  
171 };
172
173 // Create wrappers for C Binding types (see CBindingWrapping.h).
174 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef)
175
176 } // end namespace llvm
177
178 #endif