1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MemoryBuffer interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
15 #define LLVM_SUPPORT_MEMORYBUFFER_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/System/DataTypes.h"
24 /// MemoryBuffer - This interface provides simple read-only access to a block
25 /// of memory, and provides simple methods for reading files and standard input
26 /// into a memory buffer. In addition to basic access to the characters in the
27 /// file, this interface guarantees you can read one character past the end of
28 /// the file, and that this character will read as '\0'.
30 /// The '\0' guarantee is needed to support an optimization -- it's intended to
31 /// be more efficient for clients which are reading all the data to stop
32 /// reading when they encounter a '\0' than to continually check the file
33 /// position to see if it has reached the end of the file.
35 const char *BufferStart; // Start of the buffer.
36 const char *BufferEnd; // End of the buffer.
38 MemoryBuffer(const MemoryBuffer &); // DO NOT IMPLEMENT
39 MemoryBuffer &operator=(const MemoryBuffer &); // DO NOT IMPLEMENT
42 void init(const char *BufStart, const char *BufEnd);
44 virtual ~MemoryBuffer();
46 const char *getBufferStart() const { return BufferStart; }
47 const char *getBufferEnd() const { return BufferEnd; }
48 size_t getBufferSize() const { return BufferEnd-BufferStart; }
50 StringRef getBuffer() const {
51 return StringRef(BufferStart, getBufferSize());
54 /// getBufferIdentifier - Return an identifier for this buffer, typically the
55 /// filename it was read from.
56 virtual const char *getBufferIdentifier() const {
57 return "Unknown buffer";
60 /// getFile - Open the specified file as a MemoryBuffer, returning a new
61 /// MemoryBuffer if successful, otherwise returning null. If FileSize is
62 /// specified, this means that the client knows that the file exists and that
63 /// it has the specified size.
64 static MemoryBuffer *getFile(StringRef Filename,
65 std::string *ErrStr = 0,
66 int64_t FileSize = -1,
67 struct stat *FileInfo = 0);
68 static MemoryBuffer *getFile(const char *Filename,
69 std::string *ErrStr = 0,
70 int64_t FileSize = -1,
71 struct stat *FileInfo = 0);
73 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
74 /// that EndPtr[0] must be a null byte and be accessible!
75 static MemoryBuffer *getMemBuffer(StringRef InputData,
76 StringRef BufferName = "");
78 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
79 /// copying the contents and taking ownership of it. This has no requirements
81 static MemoryBuffer *getMemBufferCopy(StringRef InputData,
82 StringRef BufferName = "");
84 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
85 /// is completely initialized to zeros. Note that the caller should
86 /// initialize the memory allocated by this method. The memory is owned by
87 /// the MemoryBuffer object.
88 static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
90 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
91 /// that is not initialized. Note that the caller should initialize the
92 /// memory allocated by this method. The memory is owned by the MemoryBuffer
94 static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
95 StringRef BufferName = "");
97 /// getSTDIN - Read all of stdin into a file buffer, and return it.
98 /// If an error occurs, this returns null and fills in *ErrStr with a reason.
99 static MemoryBuffer *getSTDIN(std::string *ErrStr = 0);
102 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
103 /// if the Filename is "-". If an error occurs, this returns null and fills
104 /// in *ErrStr with a reason.
105 static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
106 std::string *ErrStr = 0,
107 int64_t FileSize = -1,
108 struct stat *FileInfo = 0);
109 static MemoryBuffer *getFileOrSTDIN(const char *Filename,
110 std::string *ErrStr = 0,
111 int64_t FileSize = -1,
112 struct stat *FileInfo = 0);
115 } // end namespace llvm