Add a Rust string ostream for LLVM

This commit is contained in:
Keegan McAllister
2014-09-09 23:12:09 -07:00
parent 77b3a7ba8b
commit 225353d8bb
4 changed files with 67 additions and 24 deletions

View File

@@ -645,22 +645,18 @@ extern "C" void LLVMDICompositeTypeSetTypeArray(
#endif
}
extern "C" char *LLVMTypeToString(LLVMTypeRef Type) {
std::string s;
llvm::raw_string_ostream os(s);
extern "C" void LLVMWriteTypeToString(LLVMTypeRef Type, RustStringRef str) {
raw_rust_string_ostream os(str);
unwrap<llvm::Type>(Type)->print(os);
return strdup(os.str().data());
}
extern "C" char *LLVMValueToString(LLVMValueRef Value) {
std::string s;
llvm::raw_string_ostream os(s);
extern "C" void LLVMWriteValueToString(LLVMValueRef Value, RustStringRef str) {
raw_rust_string_ostream os(str);
os << "(";
unwrap<llvm::Value>(Value)->getType()->print(os);
os << ":";
unwrap<llvm::Value>(Value)->print(os);
os << ")";
return strdup(os.str().data());
}
#if LLVM_VERSION_MINOR >= 5

View File

@@ -69,3 +69,31 @@
#endif
void LLVMRustSetLastError(const char*);
typedef struct OpaqueRustString *RustStringRef;
extern "C" void
rust_llvm_string_write_impl(RustStringRef str, const char *ptr, size_t size);
class raw_rust_string_ostream : public llvm::raw_ostream {
RustStringRef str;
uint64_t pos;
void write_impl(const char *ptr, size_t size) override {
rust_llvm_string_write_impl(str, ptr, size);
pos += size;
}
uint64_t current_pos() const override {
return pos;
}
public:
explicit raw_rust_string_ostream(RustStringRef str)
: str(str), pos(0) { }
~raw_rust_string_ostream() {
// LLVM requires this.
flush();
}
};