1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::{constant::Constant, context::Context, irtype::Type};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct Pointer(pub generational_arena::Index);
#[doc(hidden)]
#[derive(Clone)]
pub struct PointerContent {
pub ty: Type,
pub is_mutable: bool,
pub initializer: Option<Constant>,
}
impl Pointer {
pub fn as_string(&self, context: &Context, name: Option<&str>) -> String {
let PointerContent { ty, is_mutable, .. } = &context.pointers[self.0];
let mut_tag = if *is_mutable { "mut " } else { "" };
let name_tag = if name.is_some() {
format!(" {}", name.unwrap())
} else {
"".to_string()
};
format!("{mut_tag}ptr {}{}", ty.as_string(context), name_tag)
}
pub fn new(
context: &mut Context,
ty: Type,
is_mutable: bool,
initializer: Option<Constant>,
) -> Self {
let content = PointerContent {
ty,
is_mutable,
initializer,
};
Pointer(context.pointers.insert(content))
}
pub fn get_type<'a>(&self, context: &'a Context) -> &'a Type {
&context.pointers[self.0].ty
}
pub fn is_aggregate_ptr(&self, context: &Context) -> bool {
matches!(
&context.pointers[self.0].ty,
Type::Array(_) | Type::Struct(_) | Type::Union(_)
)
}
pub fn is_equivalent(&self, context: &Context, other: &Pointer) -> bool {
self.get_type(context).eq(context, other.get_type(context))
}
}