cl_typeck/
entry.rs

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! An [Entry] is an accessor for [nodes](Handle) in a [Table].
//!
//! There are two kinds of entry:
//! - [Entry]: Provides getters for an entry's fields, and an implementation of
//!   [Display](std::fmt::Display)
//! - [EntryMut]: Provides setters for an entry's fields, and an [`as_ref`](EntryMut::as_ref) method
//!   to demote to an [Entry].

use std::collections::HashMap;

use cl_ast::{Meta, PathPart, Sym};
use cl_structures::span::Span;

use crate::{
    handle::Handle,
    source::Source,
    stage::categorize as cat,
    table::{NodeKind, Table},
    type_expression::{self as tex, TypeExpression},
    type_kind::TypeKind,
};

mod display;

impl Handle {
    pub const fn to_entry<'t, 'a>(self, table: &'t Table<'a>) -> Entry<'t, 'a> {
        Entry { id: self, table }
    }
    pub fn to_entry_mut<'t, 'a>(self, table: &'t mut Table<'a>) -> EntryMut<'t, 'a> {
        EntryMut { id: self, table }
    }
}

#[derive(Debug)]
pub struct Entry<'t, 'a> {
    table: &'t Table<'a>,
    id: Handle,
}

impl<'t, 'a> Entry<'t, 'a> {
    pub const fn new(table: &'t Table<'a>, id: Handle) -> Self {
        Self { table, id }
    }

    pub const fn id(&self) -> Handle {
        self.id
    }

    pub fn inner(&self) -> &Table<'a> {
        self.table
    }

    pub const fn with_id(&self, id: Handle) -> Entry<'_, 'a> {
        Self { table: self.table, id }
    }

    pub fn nav(&self, path: &[PathPart]) -> Option<Entry<'_, 'a>> {
        Some(Entry { id: self.table.nav(self.id, path)?, table: self.table })
    }

    pub const fn root(&self) -> Handle {
        self.table.root()
    }

    pub fn kind(&self) -> Option<&NodeKind> {
        self.table.kind(self.id)
    }

    pub fn parent(&self) -> Option<Entry<'_, 'a>> {
        Some(Entry { id: *self.table.parent(self.id)?, ..*self })
    }

    pub fn children(&self) -> Option<&HashMap<Sym, Handle>> {
        self.table.children(self.id)
    }

    pub fn imports(&self) -> Option<&HashMap<Sym, Handle>> {
        self.table.imports(self.id)
    }

    pub fn ty(&self) -> Option<&TypeKind> {
        self.table.ty(self.id)
    }

    pub fn span(&self) -> Option<&Span> {
        self.table.span(self.id)
    }

    pub fn meta(&self) -> Option<&'a [Meta]> {
        self.table.meta(self.id)
    }

    pub fn source(&self) -> Option<&Source<'a>> {
        self.table.source(self.id)
    }

    pub fn impl_target(&self) -> Option<Entry<'_, 'a>> {
        Some(Entry { id: self.table.impl_target(self.id)?, ..*self })
    }

    pub fn selfty(&self) -> Option<Entry<'_, 'a>> {
        Some(Entry { id: self.table.selfty(self.id)?, ..*self })
    }

    pub fn name(&self) -> Option<Sym> {
        self.table.name(self.id)
    }
}

#[derive(Debug)]
pub struct EntryMut<'t, 'a> {
    table: &'t mut Table<'a>,
    id: Handle,
}

impl<'t, 'a> EntryMut<'t, 'a> {
    pub fn new(table: &'t mut Table<'a>, id: Handle) -> Self {
        Self { table, id }
    }

    pub fn as_ref(&self) -> Entry<'_, 'a> {
        Entry { table: self.table, id: self.id }
    }

    pub const fn id(&self) -> Handle {
        self.id
    }

    /// Evaluates a [TypeExpression] in this entry's context
    pub fn evaluate<Out>(&mut self, ty: &impl TypeExpression<Out>) -> Result<Out, tex::Error> {
        let Self { table, id } = self;
        ty.evaluate(table, *id)
    }

    pub fn categorize(&mut self) -> Result<(), cat::Error> {
        cat::categorize(self.table, self.id)
    }

    /// Constructs a new Handle with the provided parent [Handle]
    pub fn with_id(&mut self, parent: Handle) -> EntryMut<'_, 'a> {
        EntryMut { table: self.table, id: parent }
    }

    pub fn nav(&mut self, path: &[PathPart]) -> Option<EntryMut<'_, 'a>> {
        Some(EntryMut { id: self.table.nav(self.id, path)?, table: self.table })
    }

    pub fn new_entry(&mut self, kind: NodeKind) -> EntryMut<'_, 'a> {
        let id = self.table.new_entry(self.id, kind);
        self.with_id(id)
    }

    pub fn add_child(&mut self, name: Sym, child: Handle) -> Option<Handle> {
        self.table.add_child(self.id, name, child)
    }

    pub fn set_ty(&mut self, kind: TypeKind) -> Option<TypeKind> {
        self.table.set_ty(self.id, kind)
    }

    pub fn set_span(&mut self, span: Span) -> Option<Span> {
        self.table.set_span(self.id, span)
    }

    pub fn set_meta(&mut self, meta: &'a [Meta]) -> Option<&'a [Meta]> {
        self.table.set_meta(self.id, meta)
    }

    pub fn set_source(&mut self, source: Source<'a>) -> Option<Source<'a>> {
        self.table.set_source(self.id, source)
    }

    pub fn set_impl_target(&mut self, target: Handle) -> Option<Handle> {
        self.table.set_impl_target(self.id, target)
    }

    pub fn mark_use_item(&mut self) {
        self.table.mark_use_item(self.id)
    }

    pub fn mark_impl_item(&mut self) {
        self.table.mark_impl_item(self.id)
    }
}