aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty/primitive.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-11-26 12:42:15 +0000
committerGitHub <[email protected]>2019-11-26 12:42:15 +0000
commit36dca8de93c07be62724f973aa4c8a045941d218 (patch)
treedbe3d521c869aa12c922098e9544859343494a0e /crates/ra_hir/src/ty/primitive.rs
parent280946e597f880709d2dc2e12eb9a6db3223d979 (diff)
parent4e17718a9aaba34533ba6a46d52b4aa959c662c7 (diff)
Merge #2410
2410: Introduce hir_ty r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ty/primitive.rs')
-rw-r--r--crates/ra_hir/src/ty/primitive.rs159
1 files changed, 1 insertions, 158 deletions
diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs
index 47789db87..12dc96572 100644
--- a/crates/ra_hir/src/ty/primitive.rs
+++ b/crates/ra_hir/src/ty/primitive.rs
@@ -1,160 +1,3 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use std::fmt; 3pub use hir_ty::primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness, Uncertain};
4
5pub use hir_def::builtin_type::{FloatBitness, IntBitness, Signedness};
6
7#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
8pub enum Uncertain<T> {
9 Unknown,
10 Known(T),
11}
12
13impl From<IntTy> for Uncertain<IntTy> {
14 fn from(ty: IntTy) -> Self {
15 Uncertain::Known(ty)
16 }
17}
18
19impl fmt::Display for Uncertain<IntTy> {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 match *self {
22 Uncertain::Unknown => write!(f, "{{integer}}"),
23 Uncertain::Known(ty) => write!(f, "{}", ty),
24 }
25 }
26}
27
28impl From<FloatTy> for Uncertain<FloatTy> {
29 fn from(ty: FloatTy) -> Self {
30 Uncertain::Known(ty)
31 }
32}
33
34impl fmt::Display for Uncertain<FloatTy> {
35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 match *self {
37 Uncertain::Unknown => write!(f, "{{float}}"),
38 Uncertain::Known(ty) => write!(f, "{}", ty),
39 }
40 }
41}
42
43#[derive(Copy, Clone, Eq, PartialEq, Hash)]
44pub struct IntTy {
45 pub signedness: Signedness,
46 pub bitness: IntBitness,
47}
48
49impl fmt::Debug for IntTy {
50 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51 fmt::Display::fmt(self, f)
52 }
53}
54
55impl fmt::Display for IntTy {
56 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57 write!(f, "{}", self.ty_to_string())
58 }
59}
60
61impl IntTy {
62 pub fn isize() -> IntTy {
63 IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize }
64 }
65
66 pub fn i8() -> IntTy {
67 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 }
68 }
69
70 pub fn i16() -> IntTy {
71 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 }
72 }
73
74 pub fn i32() -> IntTy {
75 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 }
76 }
77
78 pub fn i64() -> IntTy {
79 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 }
80 }
81
82 pub fn i128() -> IntTy {
83 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 }
84 }
85
86 pub fn usize() -> IntTy {
87 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize }
88 }
89
90 pub fn u8() -> IntTy {
91 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 }
92 }
93
94 pub fn u16() -> IntTy {
95 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 }
96 }
97
98 pub fn u32() -> IntTy {
99 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 }
100 }
101
102 pub fn u64() -> IntTy {
103 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 }
104 }
105
106 pub fn u128() -> IntTy {
107 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 }
108 }
109
110 pub(crate) fn ty_to_string(self) -> &'static str {
111 match (self.signedness, self.bitness) {
112 (Signedness::Signed, IntBitness::Xsize) => "isize",
113 (Signedness::Signed, IntBitness::X8) => "i8",
114 (Signedness::Signed, IntBitness::X16) => "i16",
115 (Signedness::Signed, IntBitness::X32) => "i32",
116 (Signedness::Signed, IntBitness::X64) => "i64",
117 (Signedness::Signed, IntBitness::X128) => "i128",
118 (Signedness::Unsigned, IntBitness::Xsize) => "usize",
119 (Signedness::Unsigned, IntBitness::X8) => "u8",
120 (Signedness::Unsigned, IntBitness::X16) => "u16",
121 (Signedness::Unsigned, IntBitness::X32) => "u32",
122 (Signedness::Unsigned, IntBitness::X64) => "u64",
123 (Signedness::Unsigned, IntBitness::X128) => "u128",
124 }
125 }
126}
127
128#[derive(Copy, Clone, PartialEq, Eq, Hash)]
129pub struct FloatTy {
130 pub bitness: FloatBitness,
131}
132
133impl fmt::Debug for FloatTy {
134 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135 fmt::Display::fmt(self, f)
136 }
137}
138
139impl fmt::Display for FloatTy {
140 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141 write!(f, "{}", self.ty_to_string())
142 }
143}
144
145impl FloatTy {
146 pub fn f32() -> FloatTy {
147 FloatTy { bitness: FloatBitness::X32 }
148 }
149
150 pub fn f64() -> FloatTy {
151 FloatTy { bitness: FloatBitness::X64 }
152 }
153
154 pub(crate) fn ty_to_string(self) -> &'static str {
155 match self.bitness {
156 FloatBitness::X32 => "f32",
157 FloatBitness::X64 => "f64",
158 }
159 }
160}