aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/builtin_type.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/builtin_type.rs')
-rw-r--r--crates/ra_hir_def/src/builtin_type.rs105
1 files changed, 85 insertions, 20 deletions
diff --git a/crates/ra_hir_def/src/builtin_type.rs b/crates/ra_hir_def/src/builtin_type.rs
index 2ec0c83fe..5e8157144 100644
--- a/crates/ra_hir_def/src/builtin_type.rs
+++ b/crates/ra_hir_def/src/builtin_type.rs
@@ -30,12 +30,23 @@ pub enum FloatBitness {
30} 30}
31 31
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
33pub struct BuiltinInt {
34 pub signedness: Signedness,
35 pub bitness: IntBitness,
36}
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
39pub struct BuiltinFloat {
40 pub bitness: FloatBitness,
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
33pub enum BuiltinType { 44pub enum BuiltinType {
34 Char, 45 Char,
35 Bool, 46 Bool,
36 Str, 47 Str,
37 Int { signedness: Signedness, bitness: IntBitness }, 48 Int(BuiltinInt),
38 Float { bitness: FloatBitness }, 49 Float(BuiltinFloat),
39} 50}
40 51
41impl BuiltinType { 52impl BuiltinType {
@@ -45,22 +56,22 @@ impl BuiltinType {
45 (name::BOOL, BuiltinType::Bool), 56 (name::BOOL, BuiltinType::Bool),
46 (name::STR, BuiltinType::Str ), 57 (name::STR, BuiltinType::Str ),
47 58
48 (name::ISIZE, BuiltinType::Int { signedness: Signedness::Signed, bitness: IntBitness::Xsize }), 59 (name::ISIZE, BuiltinType::Int(BuiltinInt::ISIZE)),
49 (name::I8, BuiltinType::Int { signedness: Signedness::Signed, bitness: IntBitness::X8 }), 60 (name::I8, BuiltinType::Int(BuiltinInt::I8)),
50 (name::I16, BuiltinType::Int { signedness: Signedness::Signed, bitness: IntBitness::X16 }), 61 (name::I16, BuiltinType::Int(BuiltinInt::I16)),
51 (name::I32, BuiltinType::Int { signedness: Signedness::Signed, bitness: IntBitness::X32 }), 62 (name::I32, BuiltinType::Int(BuiltinInt::I32)),
52 (name::I64, BuiltinType::Int { signedness: Signedness::Signed, bitness: IntBitness::X64 }), 63 (name::I64, BuiltinType::Int(BuiltinInt::I64)),
53 (name::I128, BuiltinType::Int { signedness: Signedness::Signed, bitness: IntBitness::X128 }), 64 (name::I128, BuiltinType::Int(BuiltinInt::I128)),
54 65
55 (name::USIZE, BuiltinType::Int { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize }), 66 (name::USIZE, BuiltinType::Int(BuiltinInt::USIZE)),
56 (name::U8, BuiltinType::Int { signedness: Signedness::Unsigned, bitness: IntBitness::X8 }), 67 (name::U8, BuiltinType::Int(BuiltinInt::U8)),
57 (name::U16, BuiltinType::Int { signedness: Signedness::Unsigned, bitness: IntBitness::X16 }), 68 (name::U16, BuiltinType::Int(BuiltinInt::U16)),
58 (name::U32, BuiltinType::Int { signedness: Signedness::Unsigned, bitness: IntBitness::X32 }), 69 (name::U32, BuiltinType::Int(BuiltinInt::U32)),
59 (name::U64, BuiltinType::Int { signedness: Signedness::Unsigned, bitness: IntBitness::X64 }), 70 (name::U64, BuiltinType::Int(BuiltinInt::U64)),
60 (name::U128, BuiltinType::Int { signedness: Signedness::Unsigned, bitness: IntBitness::X128 }), 71 (name::U128, BuiltinType::Int(BuiltinInt::U128)),
61 72
62 (name::F32, BuiltinType::Float { bitness: FloatBitness::X32 }), 73 (name::F32, BuiltinType::Float(BuiltinFloat::F32)),
63 (name::F64, BuiltinType::Float { bitness: FloatBitness::X64 }), 74 (name::F64, BuiltinType::Float(BuiltinFloat::F64)),
64 ]; 75 ];
65} 76}
66 77
@@ -70,7 +81,7 @@ impl fmt::Display for BuiltinType {
70 BuiltinType::Char => "char", 81 BuiltinType::Char => "char",
71 BuiltinType::Bool => "bool", 82 BuiltinType::Bool => "bool",
72 BuiltinType::Str => "str", 83 BuiltinType::Str => "str",
73 BuiltinType::Int { signedness, bitness } => match (signedness, bitness) { 84 BuiltinType::Int(BuiltinInt { signedness, bitness }) => match (signedness, bitness) {
74 (Signedness::Signed, IntBitness::Xsize) => "isize", 85 (Signedness::Signed, IntBitness::Xsize) => "isize",
75 (Signedness::Signed, IntBitness::X8) => "i8", 86 (Signedness::Signed, IntBitness::X8) => "i8",
76 (Signedness::Signed, IntBitness::X16) => "i16", 87 (Signedness::Signed, IntBitness::X16) => "i16",
@@ -85,7 +96,7 @@ impl fmt::Display for BuiltinType {
85 (Signedness::Unsigned, IntBitness::X64) => "u64", 96 (Signedness::Unsigned, IntBitness::X64) => "u64",
86 (Signedness::Unsigned, IntBitness::X128) => "u128", 97 (Signedness::Unsigned, IntBitness::X128) => "u128",
87 }, 98 },
88 BuiltinType::Float { bitness } => match bitness { 99 BuiltinType::Float(BuiltinFloat { bitness }) => match bitness {
89 FloatBitness::X32 => "f32", 100 FloatBitness::X32 => "f32",
90 FloatBitness::X64 => "f64", 101 FloatBitness::X64 => "f64",
91 }, 102 },
@@ -93,3 +104,57 @@ impl fmt::Display for BuiltinType {
93 f.write_str(type_name) 104 f.write_str(type_name)
94 } 105 }
95} 106}
107
108#[rustfmt::skip]
109impl BuiltinInt {
110 pub const ISIZE: BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::Xsize };
111 pub const I8 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X8 };
112 pub const I16 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X16 };
113 pub const I32 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X32 };
114 pub const I64 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X64 };
115 pub const I128 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X128 };
116
117 pub const USIZE: BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize };
118 pub const U8 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X8 };
119 pub const U16 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X16 };
120 pub const U32 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X32 };
121 pub const U64 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X64 };
122 pub const U128 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X128 };
123
124
125 pub fn from_suffix(suffix: &str) -> Option<BuiltinInt> {
126 let res = match suffix {
127 "isize" => Self::ISIZE,
128 "i8" => Self::I8,
129 "i16" => Self::I16,
130 "i32" => Self::I32,
131 "i64" => Self::I64,
132 "i128" => Self::I128,
133
134 "usize" => Self::USIZE,
135 "u8" => Self::U8,
136 "u16" => Self::U16,
137 "u32" => Self::U32,
138 "u64" => Self::U64,
139 "u128" => Self::U128,
140
141 _ => return None,
142 };
143 Some(res)
144 }
145}
146
147#[rustfmt::skip]
148impl BuiltinFloat {
149 pub const F32: BuiltinFloat = BuiltinFloat { bitness: FloatBitness::X32 };
150 pub const F64: BuiltinFloat = BuiltinFloat { bitness: FloatBitness::X64 };
151
152 pub fn from_suffix(suffix: &str) -> Option<BuiltinFloat> {
153 let res = match suffix {
154 "f32" => BuiltinFloat::F32,
155 "f64" => BuiltinFloat::F64,
156 _ => return None,
157 };
158 Some(res)
159 }
160}