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