MemoryBuffer.h revision 333fb04506233255f10d8095c9e2de5e5f0fdc6f
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
20namespace llvm {
21
22class error_code;
23
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'.
29///
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.
34class MemoryBuffer {
35  const char *BufferStart; // Start of the buffer.
36  const char *BufferEnd;   // End of the buffer.
37
38  MemoryBuffer(const MemoryBuffer &); // DO NOT IMPLEMENT
39  MemoryBuffer &operator=(const MemoryBuffer &); // DO NOT IMPLEMENT
40protected:
41  MemoryBuffer() {}
42  void init(const char *BufStart, const char *BufEnd);
43public:
44  virtual ~MemoryBuffer();
45
46  const char *getBufferStart() const { return BufferStart; }
47  const char *getBufferEnd() const   { return BufferEnd; }
48  size_t getBufferSize() const { return BufferEnd-BufferStart; }
49
50  StringRef getBuffer() const {
51    return StringRef(BufferStart, getBufferSize());
52  }
53
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";
58  }
59
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);
68
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,
73                                   error_code &ec,
74                                   int64_t FileSize = -1);
75
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 = "");
80
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 = "");
86
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 = "");
92
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
96  /// object.
97  static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
98                                             StringRef BufferName = "");
99
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);
103
104
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
107  /// ec.
108  static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
109                                      error_code &ec,
110                                      int64_t FileSize = -1);
111  static MemoryBuffer *getFileOrSTDIN(const char *Filename,
112                                      error_code &ec,
113                                      int64_t FileSize = -1);
114};
115
116} // end namespace llvm
117
118#endif
119