Revert r127409 which broke all the Windows bots.
[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/ADT/StringRef.h"
18 #include "llvm/Support/DataTypes.h"
19
20 namespace llvm {
21
22 class error_code;
23 template<class T> class OwningPtr;
24
25 /// MemoryBuffer - This interface provides simple read-only access to a block
26 /// of memory, and provides simple methods for reading files and standard input
27 /// into a memory buffer.  In addition to basic access to the characters in the
28 /// file, this interface guarantees you can read one character past the end of
29 /// the file, and that this character will read as '\0'.
30 ///
31 /// The '\0' guarantee is needed to support an optimization -- it's intended to
32 /// be more efficient for clients which are reading all the data to stop
33 /// reading when they encounter a '\0' than to continually check the file
34 /// position to see if it has reached the end of the file.
35 class MemoryBuffer {
36   const char *BufferStart; // Start of the buffer.
37   const char *BufferEnd;   // End of the buffer.
38
39   MemoryBuffer(const MemoryBuffer &); // DO NOT IMPLEMENT
40   MemoryBuffer &operator=(const MemoryBuffer &); // DO NOT IMPLEMENT
41 protected:
42   MemoryBuffer() {}
43   void init(const char *BufStart, const char *BufEnd);
44 public:
45   virtual ~MemoryBuffer();
46
47   const char *getBufferStart() const { return BufferStart; }
48   const char *getBufferEnd() const   { return BufferEnd; }
49   size_t getBufferSize() const { return BufferEnd-BufferStart; }
50
51   StringRef getBuffer() const {
52     return StringRef(BufferStart, getBufferSize());
53   }
54
55   /// getBufferIdentifier - Return an identifier for this buffer, typically the
56   /// filename it was read from.
57   virtual const char *getBufferIdentifier() const {
58     return "Unknown buffer";
59   }
60
61   /// getFile - Open the specified file as a MemoryBuffer, returning a new
62   /// MemoryBuffer if successful, otherwise returning null.  If FileSize is
63   /// specified, this means that the client knows that the file exists and that
64   /// it has the specified size.
65   static error_code getFile(StringRef Filename, OwningPtr<MemoryBuffer> &result,
66                             int64_t FileSize = -1);
67   static error_code getFile(const char *Filename,
68                             OwningPtr<MemoryBuffer> &result,
69                             int64_t FileSize = -1);
70
71   /// getOpenFile - Given an already-open file descriptor, read the file and
72   /// return a MemoryBuffer.
73   static error_code getOpenFile(int FD, const char *Filename,
74                                 OwningPtr<MemoryBuffer> &result,
75                                 int64_t FileSize = -1);
76
77   /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
78   /// that InputData must be null terminated.
79   static MemoryBuffer *getMemBuffer(StringRef InputData,
80                                     StringRef BufferName = "");
81
82   /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
83   /// copying the contents and taking ownership of it.  InputData does not
84   /// have to be null terminated.
85   static MemoryBuffer *getMemBufferCopy(StringRef InputData,
86                                         StringRef BufferName = "");
87
88   /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
89   /// is completely initialized to zeros.  Note that the caller should
90   /// initialize the memory allocated by this method.  The memory is owned by
91   /// the MemoryBuffer object.
92   static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
93
94   /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
95   /// that is not initialized.  Note that the caller should initialize the
96   /// memory allocated by this method.  The memory is owned by the MemoryBuffer
97   /// object.
98   static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
99                                              StringRef BufferName = "");
100
101   /// getSTDIN - Read all of stdin into a file buffer, and return it.
102   /// If an error occurs, this returns null and sets ec.
103   static error_code getSTDIN(OwningPtr<MemoryBuffer> &result);
104
105
106   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
107   /// if the Filename is "-".  If an error occurs, this returns null and sets
108   /// ec.
109   static error_code getFileOrSTDIN(StringRef Filename,
110                                    OwningPtr<MemoryBuffer> &result,
111                                    int64_t FileSize = -1);
112   static error_code getFileOrSTDIN(const char *Filename,
113                                    OwningPtr<MemoryBuffer> &result,
114                                    int64_t FileSize = -1);
115 };
116
117 } // end namespace llvm
118
119 #endif