OSDN Git Service

6d6a5da180b70ce3ce63d4fef6c100191c165c9b
[android-x86/external-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 #include <system_error>
24
25 namespace llvm {
26 using std::error_code;
27 /// MemoryBuffer - This interface provides simple read-only access to a block
28 /// of memory, and provides simple methods for reading files and standard input
29 /// into a memory buffer.  In addition to basic access to the characters in the
30 /// file, this interface guarantees you can read one character past the end of
31 /// the file, and that this character will read as '\0'.
32 ///
33 /// The '\0' guarantee is needed to support an optimization -- it's intended to
34 /// be more efficient for clients which are reading all the data to stop
35 /// reading when they encounter a '\0' than to continually check the file
36 /// position to see if it has reached the end of the file.
37 class MemoryBuffer {
38   const char *BufferStart; // Start of the buffer.
39   const char *BufferEnd;   // End of the buffer.
40
41   MemoryBuffer(const MemoryBuffer &) LLVM_DELETED_FUNCTION;
42   MemoryBuffer &operator=(const MemoryBuffer &) LLVM_DELETED_FUNCTION;
43 protected:
44   MemoryBuffer() {}
45   void init(const char *BufStart, const char *BufEnd,
46             bool RequiresNullTerminator);
47 public:
48   virtual ~MemoryBuffer();
49
50   const char *getBufferStart() const { return BufferStart; }
51   const char *getBufferEnd() const   { return BufferEnd; }
52   size_t getBufferSize() const { return BufferEnd-BufferStart; }
53
54   StringRef getBuffer() const {
55     return StringRef(BufferStart, getBufferSize());
56   }
57
58   /// getBufferIdentifier - Return an identifier for this buffer, typically the
59   /// filename it was read from.
60   virtual const char *getBufferIdentifier() const {
61     return "Unknown buffer";
62   }
63
64   /// getFile - Open the specified file as a MemoryBuffer, returning a new
65   /// MemoryBuffer if successful, otherwise returning null.  If FileSize is
66   /// specified, this means that the client knows that the file exists and that
67   /// it has the specified size.
68   ///
69   /// \param IsVolatileSize Set to true to indicate that the file size may be
70   /// changing, e.g. when libclang tries to parse while the user is
71   /// editing/updating the file.
72   static error_code getFile(Twine Filename,
73                             std::unique_ptr<MemoryBuffer> &Result,
74                             int64_t FileSize = -1,
75                             bool RequiresNullTerminator = true,
76                             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.
81   ///
82   /// \param IsVolatileSize Set to true to indicate that the file size may be
83   /// changing, e.g. when libclang tries to parse while the user is
84   /// editing/updating the file.
85   static error_code getOpenFileSlice(int FD, const char *Filename,
86                                      std::unique_ptr<MemoryBuffer> &Result,
87                                      uint64_t MapSize, int64_t Offset,
88                                      bool IsVolatileSize = false);
89
90   /// Given an already-open file descriptor, read the file and return a
91   /// MemoryBuffer.
92   ///
93   /// \param IsVolatileSize Set to true to indicate that the file size may be
94   /// changing, e.g. when libclang tries to parse while the user is
95   /// editing/updating the file.
96   static error_code getOpenFile(int FD, const char *Filename,
97                                 std::unique_ptr<MemoryBuffer> &Result,
98                                 uint64_t FileSize,
99                                 bool RequiresNullTerminator = true,
100                                 bool IsVolatileSize = false);
101
102   /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
103   /// that InputData must be null terminated if RequiresNullTerminator is true.
104   static MemoryBuffer *getMemBuffer(StringRef InputData,
105                                     StringRef BufferName = "",
106                                     bool RequiresNullTerminator = true);
107
108   /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
109   /// copying the contents and taking ownership of it.  InputData does not
110   /// have to be null terminated.
111   static MemoryBuffer *getMemBufferCopy(StringRef InputData,
112                                         StringRef BufferName = "");
113
114   /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
115   /// is completely initialized to zeros.  Note that the caller should
116   /// initialize the memory allocated by this method.  The memory is owned by
117   /// the MemoryBuffer object.
118   static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
119
120   /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
121   /// that is not initialized.  Note that the caller should initialize the
122   /// memory allocated by this method.  The memory is owned by the MemoryBuffer
123   /// object.
124   static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
125                                              StringRef BufferName = "");
126
127   /// getSTDIN - Read all of stdin into a file buffer, and return it.
128   /// If an error occurs, this returns null and sets ec.
129   static error_code getSTDIN(std::unique_ptr<MemoryBuffer> &Result);
130
131
132   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
133   /// if the Filename is "-".  If an error occurs, this returns null and sets
134   /// ec.
135   static error_code getFileOrSTDIN(StringRef Filename,
136                                    std::unique_ptr<MemoryBuffer> &Result,
137                                    int64_t FileSize = -1);
138
139   //===--------------------------------------------------------------------===//
140   // Provided for performance analysis.
141   //===--------------------------------------------------------------------===//
142
143   /// The kind of memory backing used to support the MemoryBuffer.
144   enum BufferKind {
145     MemoryBuffer_Malloc,
146     MemoryBuffer_MMap
147   };
148
149   /// Return information on the memory mechanism used to support the
150   /// MemoryBuffer.
151   virtual BufferKind getBufferKind() const = 0;  
152 };
153
154 // Create wrappers for C Binding types (see CBindingWrapping.h).
155 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef)
156
157 } // end namespace llvm
158
159 #endif