|
15 | 15 |
|
16 | 16 | use std::c_str::ToCStr; |
17 | 17 | 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; |
19 | 20 | use std::option; |
20 | 21 |
|
21 | 22 | use middle::trans::type_::Type; |
@@ -1666,6 +1667,7 @@ pub mod llvm { |
1666 | 1667 | -> ValueRef; |
1667 | 1668 |
|
1668 | 1669 | pub fn LLVMDICompositeTypeSetTypeArray(CompositeType: ValueRef, TypeArray: ValueRef); |
| 1670 | + pub fn LLVMTypeToString(Type: TypeRef) -> *c_char; |
1669 | 1671 |
|
1670 | 1672 | pub fn LLVMIsAArgument(value_ref: ValueRef) -> ValueRef; |
1671 | 1673 |
|
@@ -1789,68 +1791,15 @@ impl TypeNames { |
1789 | 1791 | self.named_types.find_equiv(&s).map(|x| Type::from_ref(*x)) |
1790 | 1792 | } |
1791 | 1793 |
|
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 { |
1803 | 1795 | 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 |
1847 | 1800 | } |
1848 | 1801 | } |
1849 | 1802 |
|
1850 | | - pub fn type_to_str(&self, ty: Type) -> ~str { |
1851 | | - self.type_to_str_depth(ty, 30) |
1852 | | - } |
1853 | | - |
1854 | 1803 | pub fn types_to_str(&self, tys: &[Type]) -> ~str { |
1855 | 1804 | let strs = tys.map(|t| self.type_to_str(*t)); |
1856 | 1805 | format!("[{}]", strs.connect(",")) |
|
0 commit comments