Skip to content

Commit 35a944a

Browse files
committed
auto merge of #9815 : thestinger/rust/type, r=huonw
Example: void ({ i64, %tydesc*, i8*, i8*, i8 }*, i64*, %"struct.std::fmt::Formatter[#1]"*)* Before, we would print 20 levels deep due to recursion in the type definition.
2 parents 399a425 + 7bad416 commit 35a944a

File tree

3 files changed

+16
-59
lines changed

3 files changed

+16
-59
lines changed

src/librustc/lib/llvm.rs

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
use std::c_str::ToCStr;
1717
use std::hashmap::HashMap;
18-
use std::libc::{c_uint, c_ushort};
18+
use std::libc::{c_uint, c_ushort, c_void, free};
19+
use std::str::raw::from_c_str;
1920
use std::option;
2021

2122
use middle::trans::type_::Type;
@@ -1666,6 +1667,7 @@ pub mod llvm {
16661667
-> ValueRef;
16671668

16681669
pub fn LLVMDICompositeTypeSetTypeArray(CompositeType: ValueRef, TypeArray: ValueRef);
1670+
pub fn LLVMTypeToString(Type: TypeRef) -> *c_char;
16691671

16701672
pub fn LLVMIsAArgument(value_ref: ValueRef) -> ValueRef;
16711673

@@ -1789,68 +1791,15 @@ impl TypeNames {
17891791
self.named_types.find_equiv(&s).map(|x| Type::from_ref(*x))
17901792
}
17911793

1792-
// We have a depth count, because we seem to make infinite types.
1793-
pub fn type_to_str_depth(&self, ty: Type, depth: int) -> ~str {
1794-
match self.find_name(&ty) {
1795-
option::Some(name) => return name.to_owned(),
1796-
None => ()
1797-
}
1798-
1799-
if depth == 0 {
1800-
return ~"###";
1801-
}
1802-
1794+
pub fn type_to_str(&self, ty: Type) -> ~str {
18031795
unsafe {
1804-
let kind = ty.kind();
1805-
1806-
match kind {
1807-
Void => ~"Void",
1808-
Half => ~"Half",
1809-
Float => ~"Float",
1810-
Double => ~"Double",
1811-
X86_FP80 => ~"X86_FP80",
1812-
FP128 => ~"FP128",
1813-
PPC_FP128 => ~"PPC_FP128",
1814-
Label => ~"Label",
1815-
Vector => ~"Vector",
1816-
Metadata => ~"Metadata",
1817-
X86_MMX => ~"X86_MMAX",
1818-
Integer => {
1819-
format!("i{}", llvm::LLVMGetIntTypeWidth(ty.to_ref()) as int)
1820-
}
1821-
Function => {
1822-
let out_ty = ty.return_type();
1823-
let args = ty.func_params();
1824-
let args =
1825-
args.map(|&ty| self.type_to_str_depth(ty, depth-1)).connect(", ");
1826-
let out_ty = self.type_to_str_depth(out_ty, depth-1);
1827-
format!("fn({}) -> {}", args, out_ty)
1828-
}
1829-
Struct => {
1830-
let tys = ty.field_types();
1831-
let tys = tys.map(|&ty| self.type_to_str_depth(ty, depth-1)).connect(", ");
1832-
format!("\\{{}\\}", tys)
1833-
}
1834-
Array => {
1835-
let el_ty = ty.element_type();
1836-
let el_ty = self.type_to_str_depth(el_ty, depth-1);
1837-
let len = ty.array_length();
1838-
format!("[{} x {}]", el_ty, len)
1839-
}
1840-
Pointer => {
1841-
let el_ty = ty.element_type();
1842-
let el_ty = self.type_to_str_depth(el_ty, depth-1);
1843-
format!("*{}", el_ty)
1844-
}
1845-
_ => fail2!("Unknown Type Kind ({})", kind as uint)
1846-
}
1796+
let s = llvm::LLVMTypeToString(ty.to_ref());
1797+
let ret = from_c_str(s);
1798+
free(s as *c_void);
1799+
ret
18471800
}
18481801
}
18491802

1850-
pub fn type_to_str(&self, ty: Type) -> ~str {
1851-
self.type_to_str_depth(ty, 30)
1852-
}
1853-
18541803
pub fn types_to_str(&self, tys: &[Type]) -> ~str {
18551804
let strs = tys.map(|t| self.type_to_str(*t));
18561805
format!("[{}]", strs.connect(","))

src/rustllvm/RustWrapper.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,3 +803,10 @@ extern "C" void LLVMDICompositeTypeSetTypeArray(
803803
{
804804
unwrapDI<DICompositeType>(CompositeType).setTypeArray(unwrapDI<DIArray>(TypeArray));
805805
}
806+
807+
extern "C" char *LLVMTypeToString(LLVMTypeRef Type) {
808+
std::string s;
809+
llvm::raw_string_ostream os(s);
810+
unwrap<llvm::Type>(Type)->print(os);
811+
return strdup(os.str().data());
812+
}

src/rustllvm/rustllvm.def.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,4 @@ LLVMRustSetNormalizedTarget
628628
LLVMRustAddAlwaysInlinePass
629629
LLVMAddReturnAttribute
630630
LLVMRemoveReturnAttribute
631+
LLVMTypeToString

0 commit comments

Comments
 (0)