]>
Commit | Line | Data |
---|---|---|
064997fb FG |
1 | use std::fmt::{self, Debug}; |
2 | use std::hash::Hash; | |
3 | ||
4 | pub(crate) mod tree; | |
5 | pub(crate) use tree::Tree; | |
6 | ||
7 | pub(crate) mod nfa; | |
8 | pub(crate) use nfa::Nfa; | |
9 | ||
10 | pub(crate) mod dfa; | |
11 | pub(crate) use dfa::Dfa; | |
12 | ||
13 | #[derive(Debug)] | |
14 | pub(crate) struct Uninhabited; | |
15 | ||
16 | /// An instance of a byte is either initialized to a particular value, or uninitialized. | |
17 | #[derive(Hash, Eq, PartialEq, Clone, Copy)] | |
18 | pub(crate) enum Byte { | |
19 | Uninit, | |
20 | Init(u8), | |
21 | } | |
22 | ||
23 | impl fmt::Debug for Byte { | |
24 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
25 | match &self { | |
26 | Self::Uninit => f.write_str("??u8"), | |
9c376795 | 27 | Self::Init(b) => write!(f, "{b:#04x}u8"), |
064997fb FG |
28 | } |
29 | } | |
30 | } | |
31 | ||
c620b35d FG |
32 | pub(crate) trait Def: Debug + Hash + Eq + PartialEq + Copy + Clone { |
33 | fn has_safety_invariants(&self) -> bool; | |
34 | } | |
fe692bf9 FG |
35 | pub trait Ref: Debug + Hash + Eq + PartialEq + Copy + Clone { |
36 | fn min_align(&self) -> usize; | |
37 | ||
c620b35d FG |
38 | fn size(&self) -> usize; |
39 | ||
fe692bf9 FG |
40 | fn is_mutable(&self) -> bool; |
41 | } | |
064997fb | 42 | |
c620b35d FG |
43 | impl Def for ! { |
44 | fn has_safety_invariants(&self) -> bool { | |
45 | unreachable!() | |
46 | } | |
47 | } | |
48 | ||
fe692bf9 FG |
49 | impl Ref for ! { |
50 | fn min_align(&self) -> usize { | |
51 | unreachable!() | |
52 | } | |
c620b35d FG |
53 | fn size(&self) -> usize { |
54 | unreachable!() | |
55 | } | |
fe692bf9 FG |
56 | fn is_mutable(&self) -> bool { |
57 | unreachable!() | |
58 | } | |
59 | } | |
064997fb FG |
60 | |
61 | #[cfg(feature = "rustc")] | |
fe692bf9 | 62 | pub mod rustc { |
064997fb | 63 | use rustc_middle::mir::Mutability; |
fe692bf9 | 64 | use rustc_middle::ty::{self, Ty}; |
c620b35d | 65 | use std::fmt::{self, Write}; |
064997fb FG |
66 | |
67 | /// A reference in the layout. | |
e8be2606 | 68 | #[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)] |
064997fb | 69 | pub struct Ref<'tcx> { |
fe692bf9 FG |
70 | pub lifetime: ty::Region<'tcx>, |
71 | pub ty: Ty<'tcx>, | |
72 | pub mutability: Mutability, | |
73 | pub align: usize, | |
c620b35d | 74 | pub size: usize, |
064997fb FG |
75 | } |
76 | ||
fe692bf9 FG |
77 | impl<'tcx> super::Ref for Ref<'tcx> { |
78 | fn min_align(&self) -> usize { | |
79 | self.align | |
80 | } | |
064997fb | 81 | |
c620b35d FG |
82 | fn size(&self) -> usize { |
83 | self.size | |
84 | } | |
85 | ||
fe692bf9 FG |
86 | fn is_mutable(&self) -> bool { |
87 | match self.mutability { | |
88 | Mutability::Mut => true, | |
89 | Mutability::Not => false, | |
90 | } | |
064997fb FG |
91 | } |
92 | } | |
fe692bf9 | 93 | impl<'tcx> Ref<'tcx> {} |
064997fb | 94 | |
c620b35d FG |
95 | impl<'tcx> fmt::Display for Ref<'tcx> { |
96 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
97 | f.write_char('&')?; | |
98 | if self.mutability == Mutability::Mut { | |
99 | f.write_str("mut ")?; | |
100 | } | |
101 | self.ty.fmt(f) | |
102 | } | |
103 | } | |
104 | ||
064997fb FG |
105 | /// A visibility node in the layout. |
106 | #[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)] | |
107 | pub enum Def<'tcx> { | |
108 | Adt(ty::AdtDef<'tcx>), | |
109 | Variant(&'tcx ty::VariantDef), | |
110 | Field(&'tcx ty::FieldDef), | |
111 | Primitive, | |
112 | } | |
113 | ||
c620b35d FG |
114 | impl<'tcx> super::Def for Def<'tcx> { |
115 | fn has_safety_invariants(&self) -> bool { | |
116 | // Rust presently has no notion of 'unsafe fields', so for now we | |
117 | // make the conservative assumption that everything besides | |
118 | // primitive types carry safety invariants. | |
119 | self != &Self::Primitive | |
120 | } | |
121 | } | |
064997fb | 122 | } |