MemoryBuffer.h revision 37c7cea4847c32beb300b32e46db4805dfe716fa
1//===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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/Support/DataTypes.h"
18#include <string>
19
20namespace llvm {
21
22/// MemoryBuffer - This interface provides simple read-only access to a block
23/// of memory, and provides simple methods for reading files and standard input
24/// into a memory buffer.  In addition to basic access to the characters in the
25/// file, this interface guarantees you can read one character past the end of
26/// @verbatim the file, and that this character will read as '\0'. @endverbatim
27class MemoryBuffer {
28  const char *BufferStart; // Start of the buffer.
29  const char *BufferEnd;   // End of the buffer.
30
31  /// MustDeleteBuffer - True if we allocated this buffer.  If so, the
32  /// destructor must know the delete[] it.
33  bool MustDeleteBuffer;
34protected:
35  MemoryBuffer() : MustDeleteBuffer(false) {}
36  void init(const char *BufStart, const char *BufEnd);
37  void initCopyOf(const char *BufStart, const char *BufEnd);
38public:
39  virtual ~MemoryBuffer();
40
41  const char *getBufferStart() const { return BufferStart; }
42  const char *getBufferEnd() const   { return BufferEnd; }
43  unsigned getBufferSize() const { return BufferEnd-BufferStart; }
44
45  /// getBufferIdentifier - Return an identifier for this buffer, typically the
46  /// filename it was read from.
47  virtual const char *getBufferIdentifier() const {
48    return "Unknown buffer";
49  }
50
51  /// getFile - Open the specified file as a MemoryBuffer, returning a new
52  /// MemoryBuffer if successful, otherwise returning null.  If FileSize is
53  /// specified, this means that the client knows that the file exists and that
54  /// it has the specified size.
55  static MemoryBuffer *getFile(const char *FilenameStart, unsigned FnSize,
56                               std::string *ErrStr = 0,
57                               int64_t FileSize = -1);
58
59  /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
60  /// that EndPtr[0] must be a null byte and be accessible!
61  static MemoryBuffer *getMemBuffer(const char *StartPtr, const char *EndPtr,
62                                    const char *BufferName = "");
63
64  /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
65  /// is completely initialized to zeros.  Note that the caller should
66  /// initialize the memory allocated by this method.  The memory is owned by
67  /// the MemoryBuffer object.
68  static MemoryBuffer *getNewMemBuffer(unsigned Size,
69                                       const char *BufferName = "");
70
71  /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
72  /// that is not initialized.  Note that the caller should initialize the
73  /// memory allocated by this method.  The memory is owned by the MemoryBuffer
74  /// object.
75  static MemoryBuffer *getNewUninitMemBuffer(unsigned Size,
76                                             const char *BufferName = "");
77
78  /// getSTDIN - Read all of stdin into a file buffer, and return it.  This
79  /// fails if stdin is empty.
80  static MemoryBuffer *getSTDIN();
81
82
83  /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
84  /// if the Filename is "-".  If an error occurs, this returns null and fills
85  /// in *ErrStr with a reason.
86  static MemoryBuffer *getFileOrSTDIN(const char *FilenameStart,unsigned FnSize,
87                                      std::string *ErrStr = 0,
88                                      int64_t FileSize = -1) {
89    if (FnSize == 1 && FilenameStart[0] == '-')
90      return getSTDIN();
91    return getFile(FilenameStart, FnSize, ErrStr, FileSize);
92  }
93
94  /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
95  /// if the Filename is "-".  If an error occurs, this returns null and fills
96  /// in *ErrStr with a reason.
97  static MemoryBuffer *getFileOrSTDIN(const std::string &FN,
98                                      std::string *ErrStr = 0,
99                                      int64_t FileSize = -1) {
100    return getFileOrSTDIN(&FN[0], FN.size(), ErrStr, FileSize);
101  }
102};
103
104} // end namespace llvm
105
106#endif
107