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
//! A value representing a memory location, generally to a function local value.
//!
//! NOTE: much of this was hastily put together and can be streamlined or refactored altogether.

use crate::{constant::Constant, context::Context, irtype::Type};

/// A wrapper around an [ECS](https://github.com/fitzgen/generational-arena) handle into the
/// [`Context`].
#[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 {
    /// Return a string representation of type, used for IR printing.
    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)
    }
    /// Return a new pointer to a specific type with an optional [`Constant`] initializer.
    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))
    }

    /// Return the type pointed to by this pointer.
    pub fn get_type<'a>(&self, context: &'a Context) -> &'a Type {
        &context.pointers[self.0].ty
    }

    /// Return whether this pointer is to a [`Type::Struct`] in particular.
    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))
    }
}