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