2007-04-29 06:58:52 +00:00
|
|
|
//===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-04-29 06:58:52 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the MemoryBuffer interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_MEMORYBUFFER_H
|
|
|
|
#define LLVM_SUPPORT_MEMORYBUFFER_H
|
|
|
|
|
2009-08-24 09:29:24 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2009-10-26 01:35:46 +00:00
|
|
|
#include "llvm/System/DataTypes.h"
|
2007-05-06 23:32:36 +00:00
|
|
|
#include <string>
|
2007-04-29 06:58:52 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/// MemoryBuffer - This interface provides simple read-only access to a block
|
|
|
|
/// of memory, and provides simple methods for reading files and standard input
|
|
|
|
/// into a memory buffer. In addition to basic access to the characters in the
|
|
|
|
/// file, this interface guarantees you can read one character past the end of
|
2009-11-18 22:04:44 +00:00
|
|
|
/// the file, and that this character will read as '\0'.
|
2007-04-29 06:58:52 +00:00
|
|
|
class MemoryBuffer {
|
|
|
|
const char *BufferStart; // Start of the buffer.
|
|
|
|
const char *BufferEnd; // End of the buffer.
|
|
|
|
|
|
|
|
/// MustDeleteBuffer - True if we allocated this buffer. If so, the
|
|
|
|
/// destructor must know the delete[] it.
|
|
|
|
bool MustDeleteBuffer;
|
|
|
|
protected:
|
|
|
|
MemoryBuffer() : MustDeleteBuffer(false) {}
|
|
|
|
void init(const char *BufStart, const char *BufEnd);
|
|
|
|
void initCopyOf(const char *BufStart, const char *BufEnd);
|
|
|
|
public:
|
|
|
|
virtual ~MemoryBuffer();
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
const char *getBufferStart() const { return BufferStart; }
|
|
|
|
const char *getBufferEnd() const { return BufferEnd; }
|
2008-05-05 18:30:58 +00:00
|
|
|
size_t getBufferSize() const { return BufferEnd-BufferStart; }
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2009-08-24 09:29:24 +00:00
|
|
|
StringRef getBuffer() const {
|
|
|
|
return StringRef(BufferStart, getBufferSize());
|
|
|
|
}
|
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
/// getBufferIdentifier - Return an identifier for this buffer, typically the
|
|
|
|
/// filename it was read from.
|
|
|
|
virtual const char *getBufferIdentifier() const {
|
|
|
|
return "Unknown buffer";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getFile - Open the specified file as a MemoryBuffer, returning a new
|
|
|
|
/// MemoryBuffer if successful, otherwise returning null. If FileSize is
|
|
|
|
/// specified, this means that the client knows that the file exists and that
|
|
|
|
/// it has the specified size.
|
2009-11-10 00:43:58 +00:00
|
|
|
static MemoryBuffer *getFile(StringRef Filename,
|
2007-05-06 23:32:36 +00:00
|
|
|
std::string *ErrStr = 0,
|
2007-04-29 06:58:52 +00:00
|
|
|
int64_t FileSize = -1);
|
|
|
|
|
|
|
|
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
|
|
|
|
/// that EndPtr[0] must be a null byte and be accessible!
|
|
|
|
static MemoryBuffer *getMemBuffer(const char *StartPtr, const char *EndPtr,
|
|
|
|
const char *BufferName = "");
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2007-10-09 21:46:38 +00:00
|
|
|
/// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
|
|
|
|
/// copying the contents and taking ownership of it. This has no requirements
|
|
|
|
/// on EndPtr[0].
|
|
|
|
static MemoryBuffer *getMemBufferCopy(const char *StartPtr,const char *EndPtr,
|
|
|
|
const char *BufferName = "");
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
/// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
|
|
|
|
/// is completely initialized to zeros. Note that the caller should
|
|
|
|
/// initialize the memory allocated by this method. The memory is owned by
|
|
|
|
/// the MemoryBuffer object.
|
2008-05-05 18:30:58 +00:00
|
|
|
static MemoryBuffer *getNewMemBuffer(size_t Size,
|
2007-04-29 06:58:52 +00:00
|
|
|
const char *BufferName = "");
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
/// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
|
|
|
|
/// that is not initialized. Note that the caller should initialize the
|
|
|
|
/// memory allocated by this method. The memory is owned by the MemoryBuffer
|
|
|
|
/// object.
|
2008-05-05 18:30:58 +00:00
|
|
|
static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
|
2009-11-10 00:43:58 +00:00
|
|
|
StringRef BufferName = "");
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2009-11-10 00:43:58 +00:00
|
|
|
/// getSTDIN - Read all of stdin into a file buffer, and return it.
|
2007-04-29 06:58:52 +00:00
|
|
|
static MemoryBuffer *getSTDIN();
|
2009-02-20 22:51:36 +00:00
|
|
|
|
|
|
|
|
2007-05-06 23:32:36 +00:00
|
|
|
/// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
|
|
|
|
/// if the Filename is "-". If an error occurs, this returns null and fills
|
|
|
|
/// in *ErrStr with a reason.
|
2009-11-10 00:43:58 +00:00
|
|
|
static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
|
2007-05-06 23:32:36 +00:00
|
|
|
std::string *ErrStr = 0,
|
2009-11-10 00:43:58 +00:00
|
|
|
int64_t FileSize = -1);
|
2007-04-29 06:58:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|