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/Support/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, error_code &ec,
65 int64_t FileSize = -1);
66 static MemoryBuffer *getFile(const char *Filename, error_code &ec,
67 int64_t FileSize = -1);
69 /// getOpenFile - Given an already-open file descriptor, read the file and
70 /// return a MemoryBuffer. This takes ownership of the descriptor,
71 /// immediately closing it after reading the file.
72 static MemoryBuffer *getOpenFile(int FD, const char *Filename,
74 int64_t FileSize = -1);
76 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
77 /// that InputData must be null terminated.
78 static MemoryBuffer *getMemBuffer(StringRef InputData,
79 StringRef BufferName = "");
81 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
82 /// copying the contents and taking ownership of it. InputData does not
83 /// have to be null terminated.
84 static MemoryBuffer *getMemBufferCopy(StringRef InputData,
85 StringRef BufferName = "");
87 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
88 /// is completely initialized to zeros. Note that the caller should
89 /// initialize the memory allocated by this method. The memory is owned by
90 /// the MemoryBuffer object.
91 static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
93 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
94 /// that is not initialized. Note that the caller should initialize the
95 /// memory allocated by this method. The memory is owned by the MemoryBuffer
97 static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
98 StringRef BufferName = "");
100 /// getSTDIN - Read all of stdin into a file buffer, and return it.
101 /// If an error occurs, this returns null and sets ec.
102 static MemoryBuffer *getSTDIN(error_code &ec);
105 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
106 /// if the Filename is "-". If an error occurs, this returns null and sets
108 static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
110 int64_t FileSize = -1);
111 static MemoryBuffer *getFileOrSTDIN(const char *Filename,
113 int64_t FileSize = -1);
116 } // end namespace llvm